From 4939dc8379589630514502a34db4b3f8dfa72291 Mon Sep 17 00:00:00 2001 From: Mario Vitale Date: Thu, 28 Sep 2023 08:37:29 +0200 Subject: [PATCH 1/9] first changes to plugin, ext page module --- db/services.php | 12 ++++- externallib.php | 126 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 1 deletion(-) diff --git a/db/services.php b/db/services.php index 3ff8b94..af1210d 100644 --- a/db/services.php +++ b/db/services.php @@ -77,7 +77,16 @@ 'type' => 'write', 'ajax' => true, 'capabilities' => 'mod/folder:managefiles' - ) + ), + 'local_course_add_new_course_module_page' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_add_new_course_module_page', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Add course module Page', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/page:addinstance', + ), ); @@ -87,6 +96,7 @@ 'functions' => array( 'local_course_add_new_section', 'local_course_add_new_course_module_url', + 'local_course_add_new_course_module_page', 'local_course_add_new_course_module_resource', 'local_course_move_module_to_specific_position', 'local_course_add_new_course_module_directory', diff --git a/externallib.php b/externallib.php index de33610..7d5cbb8 100644 --- a/externallib.php +++ b/externallib.php @@ -23,6 +23,18 @@ defined('MOODLE_INTERNAL') || die(); + + +function debug($data) { + $output = $data; + if (is_array($output)) + $output = implode(',', $output); + + #echo ""; + echo "Debug Objects: " . $output . ""; +} + + /** * Class which contains the implementations of the added functions. * @@ -537,5 +549,119 @@ public static function local_sync_service_add_files_to_directory_returns() { ) ); } + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + public static function local_sync_service_add_new_course_module_page_parameters() { + return new external_function_parameters( + array( + 'courseid' => new external_value( PARAM_TEXT, 'id of course' ), + 'sectionnum' => new external_value( PARAM_TEXT, 'relative number of the section' ), + 'urlname' => new external_value( PARAM_TEXT, 'displayed mod name' ), + 'content' => new external_value( PARAM_TEXT, 'Content to insert' ), + 'time' => new external_value( PARAM_TEXT, 'defines the mod. visibility', VALUE_DEFAULT, null ), + 'visible' => new external_value( PARAM_TEXT, 'defines the mod. visibility' ), + 'beforemod' => new external_value( PARAM_TEXT, 'mod to set before', VALUE_DEFAULT, null ), + ) + ); + } + + + /** + * Method to create a new course module containing a url. + * + * @param $courseid The course id. + * @param $sectionnum The number of the section inside the course. + * @param $urlname Displayname of the Module. + * @param $content Content to publish. + * @param $time availability time. + * @param $visible visible for course members. + * @param $beforemod Optional parameter, a Module where the new Module should be placed before. + * @return $update Message: Successful and $cmid of the new Module. + */ + public static function local_sync_service_add_new_course_module_page($courseid, $sectionnum, $urlname, $content, $time = null, $visible, $beforemod = null) { + global $DB, $CFG; + require_once($CFG->dirroot . '/mod/' . '/page' . '/lib.php'); + + debug("local_sync_service_add_new_course_module_page"); + + + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_add_new_course_module_page_parameters(), + array( + 'courseid' => $courseid, + 'sectionnum' => $sectionnum, + 'urlname' => $urlname, + 'content' => $content, + 'time' => $time, + 'visible' => $visible, + 'beforemod' => $beforemod, + ) + ); + + // Ensure the current user has required permission in this course. + $context = context_course::instance($params['courseid']); + self::validate_context($context); + + + // Required permissions. + require_capability('mod/page:addinstance', $context); + + debug("prepare add instance"); + + $instance = new \stdClass(); + $instance->course = $params['courseid']; + $instance->name = $params['urlname']; + $instance->intro = null; + $instance->introformat = \FORMAT_HTML; //or FORMAT_HTML + $instance->page['format'] = PARAM_TEXT; + $instance->page = array('text' => $content, 'itemid' => false); + debug("prepare add instance 1.5"); + $instance->id = page_add_instance($instance, $instance); + + $modulename = 'content'; #TODO + + debug("prepare add instance 2"); + + $cm = new \stdClass(); + $cm->course = $params['courseid']; + $cm->module = $DB->get_field( 'modules', 'id', array('name' => $modulename) ); + $cm->instance = $instance->id; + $cm->section = $params['sectionnum']; + if (!is_null($params['time'])) { + $cm->availability = "{\"op\":\"&\",\"c\":[{\"type\":\"date\",\"d\":\">=\",\"t\":" . $params['time'] . "}],\"showc\":[" . $params['visible'] . "]}"; + } else if ( $params['visible'] === 'false' ) { + $cm->visible = 0; + } + debug("prepare add course module"); + $cm->id = add_course_module( $cm ); + $cmid = $cm->id; + debug("prepare add to section"); + + $section->id = course_add_cm_to_section($params['courseid'], $cmid, $params['sectionnum'], $params['beforemod']); + + $update = [ + 'message' => 'Successful', + 'id' => $cmid, + ]; + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_add_new_course_module_page_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'id' => new external_value( PARAM_TEXT, 'cmid of the new module' ), + ) + ); + } + } From efd787e08d65cacac4cfac9185b5154650ec8a9f Mon Sep 17 00:00:00 2001 From: Mario Vitale Date: Thu, 28 Sep 2023 10:08:51 +0200 Subject: [PATCH 2/9] working add page module with Markdown --- externallib.php | 50 +++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/externallib.php b/externallib.php index 7d5cbb8..3f4395e 100644 --- a/externallib.php +++ b/externallib.php @@ -192,7 +192,7 @@ public static function local_sync_service_add_new_course_module_url($courseid, $ $cm->id = add_course_module( $cm ); $cmid = $cm->id; - $section->id = course_add_cm_to_section($params['courseid'], $cmid, $params['sectionnum'], $params['beforemod']); + $sectionid = course_add_cm_to_section($params['courseid'], $cmid, $params['sectionnum'], $params['beforemod']); $update = [ 'message' => 'Successful', @@ -294,7 +294,7 @@ public static function local_sync_service_add_new_course_module_resource($course $instance->files = $params['itemid']; $instance->id = resource_add_instance($instance, null); - $section->id = course_add_cm_to_section($params['courseid'], $cmid, $params['sectionnum'], $params['beforemod']); + $sectionid = course_add_cm_to_section($params['courseid'], $cmid, $params['sectionnum'], $params['beforemod']); $update = [ 'message' => 'Successful', @@ -465,7 +465,7 @@ public static function local_sync_service_add_new_course_module_directory($cours $instance->files = $params['itemid']; $instance->id = folder_add_instance($instance, null); - $section->id = course_add_cm_to_section($params['courseid'], $cmid, $params['sectionnum'], $params['beforemod']); + $sectionid = course_add_cm_to_section($params['courseid'], $cmid, $params['sectionnum'], $params['beforemod']); $update = [ 'message' => 'Successful', @@ -610,43 +610,49 @@ public static function local_sync_service_add_new_course_module_page($courseid, // Required permissions. require_capability('mod/page:addinstance', $context); - debug("prepare add instance"); - - $instance = new \stdClass(); - $instance->course = $params['courseid']; - $instance->name = $params['urlname']; - $instance->intro = null; - $instance->introformat = \FORMAT_HTML; //or FORMAT_HTML - $instance->page['format'] = PARAM_TEXT; - $instance->page = array('text' => $content, 'itemid' => false); - debug("prepare add instance 1.5"); - $instance->id = page_add_instance($instance, $instance); - - $modulename = 'content'; #TODO - - debug("prepare add instance 2"); + debug("prepare add course module"); + $modulename = 'page'; #TODO $cm = new \stdClass(); $cm->course = $params['courseid']; $cm->module = $DB->get_field( 'modules', 'id', array('name' => $modulename) ); - $cm->instance = $instance->id; + //$cm->instance = $instance->id; $cm->section = $params['sectionnum']; if (!is_null($params['time'])) { $cm->availability = "{\"op\":\"&\",\"c\":[{\"type\":\"date\",\"d\":\">=\",\"t\":" . $params['time'] . "}],\"showc\":[" . $params['visible'] . "]}"; } else if ( $params['visible'] === 'false' ) { $cm->visible = 0; } - debug("prepare add course module"); + $cm->id = add_course_module( $cm ); $cmid = $cm->id; - debug("prepare add to section"); + debug("course module added $cmid\n"); - $section->id = course_add_cm_to_section($params['courseid'], $cmid, $params['sectionnum'], $params['beforemod']); + $instance = new \stdClass(); + $instance->course = $params['courseid']; + $instance->name = $params['urlname']; + $instance->intro = null; + $instance->introformat = \FORMAT_HTML; //or FORMAT_HTML + $instance->intro = '

'.$params['urlname'].'

'; + $instance->page = array('format' => \FORMAT_MARKDOWN,'text' => $content, 'itemid' => false); + $instance->coursemodule = $cmid; + $instance->id = page_add_instance($instance, $instance); + + + debug("prepare add to section\n"); + print_r ($params, false); + + $secsectionid = course_add_cm_to_section($params['courseid'], $cmid, $params['sectionnum'], $params['beforemod']); + + debug("prepare add to section done $sectionid\n"); $update = [ 'message' => 'Successful', 'id' => $cmid, ]; + + debug("prepare add to section done2\n"); + return $update; } From bfef1749d7c30d5160ab98d8d2c9d08b3a5987c5 Mon Sep 17 00:00:00 2001 From: Mario Vitale Date: Fri, 29 Sep 2023 14:30:30 +0200 Subject: [PATCH 3/9] working module book --- db/services.php | 10 ++++ externallib.php | 133 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 129 insertions(+), 14 deletions(-) diff --git a/db/services.php b/db/services.php index af1210d..21366c8 100644 --- a/db/services.php +++ b/db/services.php @@ -87,6 +87,15 @@ 'ajax' => true, 'capabilities' => 'mod/page:addinstance', ), + 'local_course_add_new_course_module_book' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_add_new_course_module_book', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Add course module Book', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/book:addinstance', + ), ); @@ -97,6 +106,7 @@ 'local_course_add_new_section', 'local_course_add_new_course_module_url', 'local_course_add_new_course_module_page', + 'local_course_add_new_course_module_book', 'local_course_add_new_course_module_resource', 'local_course_move_module_to_specific_position', 'local_course_add_new_course_module_directory', diff --git a/externallib.php b/externallib.php index 3f4395e..53f6741 100644 --- a/externallib.php +++ b/externallib.php @@ -570,7 +570,7 @@ public static function local_sync_service_add_new_course_module_page_parameters( /** - * Method to create a new course module containing a url. + * Method to create a new course module containing a Page. * * @param $courseid The course id. * @param $sectionnum The number of the section inside the course. @@ -610,13 +610,10 @@ public static function local_sync_service_add_new_course_module_page($courseid, // Required permissions. require_capability('mod/page:addinstance', $context); - debug("prepare add course module"); - $modulename = 'page'; #TODO - + $modulename = 'page'; $cm = new \stdClass(); $cm->course = $params['courseid']; $cm->module = $DB->get_field( 'modules', 'id', array('name' => $modulename) ); - //$cm->instance = $instance->id; $cm->section = $params['sectionnum']; if (!is_null($params['time'])) { $cm->availability = "{\"op\":\"&\",\"c\":[{\"type\":\"date\",\"d\":\">=\",\"t\":" . $params['time'] . "}],\"showc\":[" . $params['visible'] . "]}"; @@ -626,33 +623,141 @@ public static function local_sync_service_add_new_course_module_page($courseid, $cm->id = add_course_module( $cm ); $cmid = $cm->id; - debug("course module added $cmid\n"); $instance = new \stdClass(); $instance->course = $params['courseid']; $instance->name = $params['urlname']; $instance->intro = null; - $instance->introformat = \FORMAT_HTML; //or FORMAT_HTML + $instance->introformat = \FORMAT_HTML; $instance->intro = '

'.$params['urlname'].'

'; $instance->page = array('format' => \FORMAT_MARKDOWN,'text' => $content, 'itemid' => false); $instance->coursemodule = $cmid; $instance->id = page_add_instance($instance, $instance); + $secsectionid = course_add_cm_to_section($params['courseid'], $cmid, $params['sectionnum'], $params['beforemod']); + + $update = [ + 'message' => 'Successful', + 'id' => $cmid, + ]; + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_add_new_course_module_page_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'id' => new external_value( PARAM_TEXT, 'cmid of the new module' ), + ) + ); + } + + // + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + public static function local_sync_service_add_new_course_module_book_parameters() { + return new external_function_parameters( + array( + 'courseid' => new external_value( PARAM_TEXT, 'id of course' ), + 'sectionnum' => new external_value( PARAM_TEXT, 'relative number of the section' ), + 'urlname' => new external_value( PARAM_TEXT, 'displayed mod name' ), + 'content' => new external_value( PARAM_TEXT, 'Content to insert' ), + 'time' => new external_value( PARAM_TEXT, 'defines the mod. visibility', VALUE_DEFAULT, null ), + 'visible' => new external_value( PARAM_TEXT, 'defines the mod. visibility' ), + 'beforemod' => new external_value( PARAM_TEXT, 'mod to set before', VALUE_DEFAULT, null ), + ) + ); + } + + + /** + * Method to create a new course module containing a book. + * + * @param $courseid The course id. + * @param $sectionnum The number of the section inside the course. + * @param $urlname Displayname of the Module. + * @param $content Content to publish. + * @param $time availability time. + * @param $visible visible for course members. + * @param $beforemod Optional parameter, a Module where the new Module should be placed before. + * @return $update Message: Successful and $cmid of the new Module. + */ + public static function local_sync_service_add_new_course_module_book($courseid, $sectionnum, $urlname, $content, $time = null, $visible, $beforemod = null) { + global $DB, $CFG; + require_once($CFG->dirroot . '/mod/' . '/book' . '/lib.php'); + require_once($CFG->dirroot . '/mod/' . '/book' . '/locallib.php'); + + debug("local_sync_service_add_new_course_module_book"); - debug("prepare add to section\n"); - print_r ($params, false); + + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_add_new_course_module_book_parameters(), + array( + 'courseid' => $courseid, + 'sectionnum' => $sectionnum, + 'urlname' => $urlname, + 'content' => $content, + 'time' => $time, + 'visible' => $visible, + 'beforemod' => $beforemod, + ) + ); + + // Ensure the current user has required permission in this course. + $context = context_course::instance($params['courseid']); + self::validate_context($context); + + + // Required permissions. + require_capability('mod/book:addinstance', $context); + + $instance = new \stdClass(); + $instance->course = $params['courseid']; + $instance->name = $params['urlname']; + $instance->introformat = \FORMAT_HTML; + $instance->completionexpected=null; //todo + $instance->intro = '

'.$params['urlname'].'

'; + //$instance->book = array('format' => \FORMAT_MARKDOWN,'text' => $content, 'itemid' => false); + //$instance->coursemodule = $cmid; + $instance->visible=1; + $instance->id = book_add_instance($instance, null); + + debug("added book $instance->id"); + + + debug("prepare add course module"); + $modulename = 'book'; + $cm = new \stdClass(); + $cm->course = $params['courseid']; + $cm->instance = $instance->id; + $cm->module = $DB->get_field( 'modules', 'id', array('name' => $modulename) ); + $cm->section = $params['sectionnum']; + if (!is_null($params['time'])) { + $cm->availability = "{\"op\":\"&\",\"c\":[{\"type\":\"date\",\"d\":\">=\",\"t\":" . $params['time'] . "}],\"showc\":[" . $params['visible'] . "]}"; + } else if ( $params['visible'] === 'false' ) { + $cm->visible = 0; + } + + $cm->id = add_course_module( $cm ); + $cmid = $cm->id; + debug("course module added $cmid\n"); $secsectionid = course_add_cm_to_section($params['courseid'], $cmid, $params['sectionnum'], $params['beforemod']); - debug("prepare add to section done $sectionid\n"); + debug("prepare add to section done $sectionid "); $update = [ 'message' => 'Successful', 'id' => $cmid, ]; - - debug("prepare add to section done2\n"); - return $update; } @@ -660,7 +765,7 @@ public static function local_sync_service_add_new_course_module_page($courseid, * Obtains the Parameter which will be returned. * @return external_description */ - public static function local_sync_service_add_new_course_module_page_returns() { + public static function local_sync_service_add_new_course_module_book_returns() { return new external_single_structure( array( 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), From 77a126451940a17df5cfa0ce7d8c7139316322d7 Mon Sep 17 00:00:00 2001 From: Mario Vitale Date: Fri, 29 Sep 2023 17:44:23 +0200 Subject: [PATCH 4/9] add local_course_import_html_in_book functionality --- db/services.php | 10 ++++++ externallib.php | 92 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 95 insertions(+), 7 deletions(-) diff --git a/db/services.php b/db/services.php index 21366c8..25a7fd3 100644 --- a/db/services.php +++ b/db/services.php @@ -96,6 +96,15 @@ 'ajax' => true, 'capabilities' => 'mod/book:addinstance', ), + 'local_course_import_html_in_book' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_import_html_in_book', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Upload chapters in book', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'booktool/importhtml:import', + ), ); @@ -107,6 +116,7 @@ 'local_course_add_new_course_module_url', 'local_course_add_new_course_module_page', 'local_course_add_new_course_module_book', + 'local_course_import_html_in_book', 'local_course_add_new_course_module_resource', 'local_course_move_module_to_specific_position', 'local_course_add_new_course_module_directory', diff --git a/externallib.php b/externallib.php index 53f6741..18a1d33 100644 --- a/externallib.php +++ b/externallib.php @@ -656,7 +656,6 @@ public static function local_sync_service_add_new_course_module_page_returns() { ); } - // /** * Defines the necessary method parameters. @@ -696,7 +695,6 @@ public static function local_sync_service_add_new_course_module_book($courseid, debug("local_sync_service_add_new_course_module_book"); - // Parameter validation. $params = self::validate_parameters( self::local_sync_service_add_new_course_module_book_parameters(), @@ -715,25 +713,21 @@ public static function local_sync_service_add_new_course_module_book($courseid, $context = context_course::instance($params['courseid']); self::validate_context($context); - // Required permissions. require_capability('mod/book:addinstance', $context); + $instance = new \stdClass(); $instance->course = $params['courseid']; $instance->name = $params['urlname']; $instance->introformat = \FORMAT_HTML; $instance->completionexpected=null; //todo $instance->intro = '

'.$params['urlname'].'

'; - //$instance->book = array('format' => \FORMAT_MARKDOWN,'text' => $content, 'itemid' => false); - //$instance->coursemodule = $cmid; $instance->visible=1; $instance->id = book_add_instance($instance, null); debug("added book $instance->id"); - - debug("prepare add course module"); $modulename = 'book'; $cm = new \stdClass(); $cm->course = $params['courseid']; @@ -773,6 +767,90 @@ public static function local_sync_service_add_new_course_module_book_returns() { ) ); } +// + + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + public static function local_sync_service_import_html_in_book_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value( PARAM_TEXT, 'course module id of book' ), + 'itemid' => new external_value( PARAM_TEXT, 'itemid containing preloaded zip file to import in book' ), + 'type' => new external_value( PARAM_TEXT, 'type (typezipdirs or typezipfiles)' ) + ) + ); + } + + + /** + * Method to upload ZIP file in book so it appears as chapters in Moodle + * + * @param $cmid Course module id + * @param $itemid Item id + * @param $type Type of import + * @return $update Message: Successful and return value 0 if ok + */ + public static function local_sync_service_import_html_in_book($cmid, $itemid, $type) { + global $DB, $CFG, $USER; + require_once($CFG->dirroot . '/mod/' . '/book' . '/lib.php'); + require_once($CFG->dirroot . '/mod/' . '/book' . '/locallib.php'); + require_once($CFG->dirroot . '/mod/' . '/book/tool/importhtml' . '/locallib.php'); + + debug("local_sync_service_import_html_in_book"); + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_import_html_in_book_parameters(), + array( + 'cmid' => $bookid, + 'itemid' => $itemid, + 'type' => $type + ) + ); + + // Ensure the current user has required permission in this course. + $cm = get_coursemodule_from_id('book', $cmid, 0, false, MUST_EXIST); + $context = context_module::instance($cm->id); + self::validate_context($context); + $book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST); + + require_capability('booktool/importhtml:import', $context); + + $fs = get_file_storage(); + debug("get info about itemid $itemid"); + if (!$files = $fs->get_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $itemid, 'id', false)) { + debug("no itemid $itemid found"); + $update = ['message' => 'Itemid not found','rv' => -1]; + } + else { + $file = reset($files); + if ($file->get_mimetype() != 'application/zip') { + debug("$itemid is not a zip content"); + $update = ['message' => 'Not a zip content','rv' => -1]; + } + else{ + debug("all clear, let's go"); + toolbook_importhtml_import_chapters($file, $type, $book, $context, false); + $update = ['message' => 'Successful','rv' => 0]; + } + } + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_import_html_in_book_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'rv' => new external_value( PARAM_TEXT, 'return value' ), + ) + ); + } } From cbadf54fa7849550e9dfe9781e3e28fb6d098763 Mon Sep 17 00:00:00 2001 From: Mario Vitale Date: Wed, 4 Oct 2023 14:14:43 +0200 Subject: [PATCH 5/9] added method local_course_delete_all_chapters_from_book --- db/services.php | 10 ++++ externallib.php | 120 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 129 insertions(+), 1 deletion(-) diff --git a/db/services.php b/db/services.php index 25a7fd3..7fb0723 100644 --- a/db/services.php +++ b/db/services.php @@ -105,6 +105,15 @@ 'ajax' => true, 'capabilities' => 'booktool/importhtml:import', ), + 'local_course_delete_all_chapters_from_book' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_delete_all_chapters_from_book', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Delete all chapters from book', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/book:edit', + ), ); @@ -117,6 +126,7 @@ 'local_course_add_new_course_module_page', 'local_course_add_new_course_module_book', 'local_course_import_html_in_book', + 'local_course_delete_all_chapters_from_book', 'local_course_add_new_course_module_resource', 'local_course_move_module_to_specific_position', 'local_course_add_new_course_module_directory', diff --git a/externallib.php b/externallib.php index 18a1d33..422a3ae 100644 --- a/externallib.php +++ b/externallib.php @@ -804,7 +804,7 @@ public static function local_sync_service_import_html_in_book($cmid, $itemid, $t $params = self::validate_parameters( self::local_sync_service_import_html_in_book_parameters(), array( - 'cmid' => $bookid, + 'cmid' => $cmid, 'itemid' => $itemid, 'type' => $type ) @@ -852,5 +852,123 @@ public static function local_sync_service_import_html_in_book_returns() { ); } +// + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + public static function local_sync_service_delete_all_chapters_from_book_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value( PARAM_TEXT, 'course module id of book' ) + ) + ); + } + + + /** + * Method to delete all chapters in a book + * + * @param $cmid Course module id + * @return $update Message: Successful and return value 0 if ok + */ + public static function local_sync_service_delete_all_chapters_from_book($cmid) { + global $DB, $CFG, $USER; + require_once($CFG->dirroot . '/mod/' . '/book' . '/lib.php'); + require_once($CFG->dirroot . '/mod/' . '/book' . '/locallib.php'); + + debug("local_course_delete_all_chapters_from_book\n"); + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_delete_all_chapters_from_book_parameters(), + array( + 'cmid' => $cmid, + ) + ); + + // Ensure the current user has required permission in this course. + $cm = get_coursemodule_from_id('book', $cmid, 0, false, MUST_EXIST); + debug("module id $cm->id\n"); + $context = context_module::instance($cm->id); + self::validate_context($context); + + require_capability('mod/book:edit', $context); + + $fs = get_file_storage(); + $book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST); + debug("book id $book->id\n"); + + //$chapterid = 13; + //$chapter = $DB->get_record('book_chapters', ['id' => $chapterid, 'bookid' => $book->id], '*', MUST_EXIST); + //$chapter = $DB->get_records('book_chapters', ['bookid' => $book->id], '*', MUST_EXIST); + $chapter = $DB->get_records('book_chapters', array('bookid' => $book->id), 'pagenum', 'id, pagenum,subchapter, title, content, contentformat, hidden'); + foreach ($chapter as $id => $ch) { + debug("in chapter $ch->id\n"); + + + $subchaptercount = 0; + if (!$chapter->subchapter) { + // This is a top-level chapter. + // Make sure to remove any sub-chapters if there are any. + $chapters = $DB->get_recordset_select('book_chapters', 'bookid = :bookid AND pagenum > :pagenum', [ + 'bookid' => $book->id, + 'pagenum' => $chapter->pagenum, + ], 'pagenum'); + + foreach ($chapters as $ch) { + debug("get chapter $ch->id\n"); + if (!$ch->subchapter) { + // This is a new chapter. Any subsequent subchapters will be part of a different chapter. + break; + } else { + // This is subchapter of the chapter being removed. + core_tag_tag::remove_all_item_tags('mod_book', 'book_chapters', $ch->id); + $fs->delete_area_files($context->id, 'mod_book', 'chapter', $ch->id); + $DB->delete_records('book_chapters', ['id' => $ch->id]); + + $subchaptercount++; + } + } + $chapters->close(); + } + else + debug("no subcharters to delete\n"); + + // Now delete the actual chapter. + debug("delete chapter $ch->id\n"); + core_tag_tag::remove_all_item_tags('mod_book', 'book_chapters', $ch->id); + $fs->delete_area_files($context->id, 'mod_book', 'chapter', $ch->id); + $DB->delete_records('book_chapters', ['id' => $ch->id]); + } + + // Ensure that the book structure is correct. + // book_preload_chapters will fix parts including the pagenum. + $chapters = book_preload_chapters($book); + + book_add_fake_block($chapters, $chapter, $book, $cm); + + // Bump the book revision. + $DB->set_field('book', 'revision', $book->revision + 1, ['id' => $book->id]); + + debug("all clear, let's go"); + $update = ['message' => 'Successful','rv' => 0]; + + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_delete_all_chapters_from_book_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'rv' => new external_value( PARAM_TEXT, 'return value' ), + ) + ); + } + } From 27e20dee3ed95dccd78907f9f2e4ca1be7da4f77 Mon Sep 17 00:00:00 2001 From: Mario Vitale Date: Wed, 18 Oct 2023 16:11:08 +0200 Subject: [PATCH 6/9] added local_sync_service_update_course_module_resource method for being able to update a Resource modules --- db/services.php | 11 ++++++ externallib.php | 93 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 101 insertions(+), 3 deletions(-) diff --git a/db/services.php b/db/services.php index 7fb0723..cc81000 100644 --- a/db/services.php +++ b/db/services.php @@ -114,6 +114,16 @@ 'ajax' => true, 'capabilities' => 'mod/book:edit', ), + 'local_course_update_course_module_resource' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_update_course_module_resource', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Update course module resource', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/resource:addinstance', + ), + ); @@ -128,6 +138,7 @@ 'local_course_import_html_in_book', 'local_course_delete_all_chapters_from_book', 'local_course_add_new_course_module_resource', + 'local_course_update_course_module_resource', 'local_course_move_module_to_specific_position', 'local_course_add_new_course_module_directory', 'local_course_add_files_to_directory', diff --git a/externallib.php b/externallib.php index 422a3ae..022079f 100644 --- a/externallib.php +++ b/externallib.php @@ -899,9 +899,6 @@ public static function local_sync_service_delete_all_chapters_from_book($cmid) { $book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST); debug("book id $book->id\n"); - //$chapterid = 13; - //$chapter = $DB->get_record('book_chapters', ['id' => $chapterid, 'bookid' => $book->id], '*', MUST_EXIST); - //$chapter = $DB->get_records('book_chapters', ['bookid' => $book->id], '*', MUST_EXIST); $chapter = $DB->get_records('book_chapters', array('bookid' => $book->id), 'pagenum', 'id, pagenum,subchapter, title, content, contentformat, hidden'); foreach ($chapter as $id => $ch) { debug("in chapter $ch->id\n"); @@ -970,5 +967,95 @@ public static function local_sync_service_delete_all_chapters_from_book_returns( ); } + + + //local_sync_service_update_course_module_resource + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + public static function local_sync_service_update_course_module_resource_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value( PARAM_TEXT, 'id of module' ), + 'itemid' => new external_value( PARAM_TEXT, 'id of the upload' ), + 'displayname' => new external_value( PARAM_TEXT, 'displayed mod name', VALUE_DEFAULT, null ) + ) + ); + } + + /** + * Method to update a new course module containing a file. + * + * @param $courseid The course id. + * @param $itemid File to publish. + * @param $displayname Displayname of resource (optional) + * @return $update Message: Successful and $cmid of the new Module. + */ + public static function local_sync_service_update_course_module_resource($cmid, $itemid, $displayname) { + global $DB, $CFG; + require_once($CFG->dirroot . '/mod/' . '/resource' . '/lib.php'); + require_once($CFG->dirroot . '/mod/' . '/resource' . '/locallib.php'); + require_once($CFG->dirroot . '/availability/' . '/condition' . '/date' . '/classes' . '/condition.php'); + + debug("local_sync_service_update_course_module_resource\n"); + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_update_course_module_resource_parameters(), + array( + 'cmid' => $cmid, + 'itemid' => $itemid, + 'displayname' => $displayname + ) + ); + + $cm = get_coursemodule_from_id('resource', $cmid, 0, false, MUST_EXIST); + debug("module instance id $cm->instance\n"); + + // Ensure the current user has required permission in this course. + $context = context_course::instance($cm->course); + self::validate_context($context); + + // Required permissions. + require_capability('mod/resource:addinstance', $context); + + $modulename = 'resource'; + $instance = new \stdClass(); + $instance->course = $cm->course; + $instance->intro = null; + $instance->introformat = \FORMAT_HTML; + $instance->coursemodule = $cmid; + $instance->files = $params['itemid']; + $instance->instance = $cm->instance; + //display name is optional + if (!is_null($params['displayname'])) { + $instance->name = $params['displayname']; + } else { + $instance->name = $cm->name; + } + + $instance->id = resource_update_instance($instance, null); + + $update = [ + 'message' => 'Successful', + 'id' => $cmid, + ]; + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_update_course_module_resource_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'id' => new external_value( PARAM_TEXT, 'cmid of the new module' ), + ) + ); + } + } From 8dd7f6d5f5eb46124c74a3d3f9516e0eb4ef68e4 Mon Sep 17 00:00:00 2001 From: Mario Vitale Date: Wed, 18 Oct 2023 16:56:52 +0200 Subject: [PATCH 7/9] added core functions to services list this way the orchestrator can do all it's work using 1 single token --- db/services.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/db/services.php b/db/services.php index cc81000..c420bc5 100644 --- a/db/services.php +++ b/db/services.php @@ -142,11 +142,14 @@ 'local_course_move_module_to_specific_position', 'local_course_add_new_course_module_directory', 'local_course_add_files_to_directory', + 'core_course_get_courses', 'core_course_get_contents', + 'core_course_get_course_module', 'core_enrol_get_users_courses', 'core_webservice_get_site_info', 'core_course_delete_modules', - 'core_course_get_user_administration_options' + 'core_course_get_user_administration_options', + ), 'restrictedusers' => 1, 'enabled' => 1, From c8e7daa4ca1644c11e52b87330e99440a6c279e5 Mon Sep 17 00:00:00 2001 From: Mario Vitale Date: Wed, 25 Oct 2023 16:35:24 +0200 Subject: [PATCH 8/9] added local_course_update_course_module_label method + fix so that the changes are shown immediately --- db/services.php | 10 +++++ externallib.php | 98 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 103 insertions(+), 5 deletions(-) diff --git a/db/services.php b/db/services.php index c420bc5..95a0ccd 100644 --- a/db/services.php +++ b/db/services.php @@ -123,6 +123,15 @@ 'ajax' => true, 'capabilities' => 'mod/resource:addinstance', ), + 'local_course_update_course_module_label' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_update_course_module_label', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Update course module of type label (aka text&media)', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/label:addinstance', + ), @@ -139,6 +148,7 @@ 'local_course_delete_all_chapters_from_book', 'local_course_add_new_course_module_resource', 'local_course_update_course_module_resource', + 'local_course_update_course_module_label', 'local_course_move_module_to_specific_position', 'local_course_add_new_course_module_directory', 'local_course_add_files_to_directory', diff --git a/externallib.php b/externallib.php index 022079f..75c1cd2 100644 --- a/externallib.php +++ b/externallib.php @@ -968,9 +968,6 @@ public static function local_sync_service_delete_all_chapters_from_book_returns( } - - //local_sync_service_update_course_module_resource - /** * Defines the necessary method parameters. * @return external_function_parameters @@ -997,6 +994,7 @@ public static function local_sync_service_update_course_module_resource($cmid, $ global $DB, $CFG; require_once($CFG->dirroot . '/mod/' . '/resource' . '/lib.php'); require_once($CFG->dirroot . '/mod/' . '/resource' . '/locallib.php'); + require_once($CFG->dirroot . '/course/' . '/modlib.php'); require_once($CFG->dirroot . '/availability/' . '/condition' . '/date' . '/classes' . '/condition.php'); debug("local_sync_service_update_course_module_resource\n"); @@ -1023,11 +1021,13 @@ public static function local_sync_service_update_course_module_resource($cmid, $ $modulename = 'resource'; $instance = new \stdClass(); $instance->course = $cm->course; - $instance->intro = null; + $instance->intro = ""; $instance->introformat = \FORMAT_HTML; $instance->coursemodule = $cmid; - $instance->files = $params['itemid']; + $instance->files = $itemid; $instance->instance = $cm->instance; + $instance->modulename = $modulename; + $instance->type = 'mod'; //display name is optional if (!is_null($params['displayname'])) { $instance->name = $params['displayname']; @@ -1036,6 +1036,8 @@ public static function local_sync_service_update_course_module_resource($cmid, $ } $instance->id = resource_update_instance($instance, null); + $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); + $moduleinfo = edit_module_post_actions($instance, $course); $update = [ 'message' => 'Successful', @@ -1057,5 +1059,91 @@ public static function local_sync_service_update_course_module_resource_returns( ); } + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + public static function local_sync_service_update_course_module_label_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value( PARAM_TEXT, 'id of module' ), + 'htmlbody' => new external_value( PARAM_TEXT, 'HTML name', VALUE_DEFAULT, null ), + ) + ); + } + + /** + * Method to update a new course module containing a file. + * + * @param $cmid The course module id. + * @param $htmlbody HTML code to add to body + * @return $update Message: Successful and $cmid of the new Module. + */ + public static function local_sync_service_update_course_module_label($cmid, $htmlbody) { + global $DB, $CFG; + require_once($CFG->dirroot . '/availability/' . '/condition' . '/date' . '/classes' . '/condition.php'); + require_once($CFG->dirroot . '/mod/' . '/label' . '/lib.php'); + require_once($CFG->dirroot . '/course/' . '/modlib.php'); + + debug("local_sync_service_update_course_module_label\n"); + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_update_course_module_label_parameters(), + array( + 'cmid' => $cmid, + 'htmlbody' => $htmlbody + ) + ); + + $cm = get_coursemodule_from_id('label', $cmid, 0, false, MUST_EXIST); + + // Ensure the current user has required permission in this course. + $context = context_course::instance($cm->course); + self::validate_context($context); + + + // Required permissions. + require_capability('mod/label:addinstance', $context); + + $modulename = 'label'; + $cm->module = $DB->get_field( 'modules', 'id', array('name' => $modulename) ); + $instance = new \stdClass(); + $instance->course = $cm->course; + $instance->intro = $htmlbody; + + $instance->introformat = \FORMAT_HTML; + $instance->coursemodule = $cmid; + $instance->instance = $cm->instance; + $instance->modulename = $modulename; + $instance->type = 'mod'; + $instance->visible = true; + $instance->id = label_update_instance($instance, null); + + $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); + + $moduleinfo = edit_module_post_actions($instance, $course); + + $update = [ + 'message' => 'Successful', + 'id' => $cmid, + ]; + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_update_course_module_label_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'id' => new external_value( PARAM_TEXT, 'cmid of the new module' ), + ) + ); + } + + } From a80daa3ecf0904deca8c8ab5e7b1631fa8378119 Mon Sep 17 00:00:00 2001 From: Mario Vitale Date: Mon, 8 Jul 2024 10:34:17 +0200 Subject: [PATCH 9/9] added new methods --- db/services.php | 91 ++++- externallib.php | 884 ++++++++++++++++++++++++++++++++++++++++++++++++ version.php | 2 +- 3 files changed, 975 insertions(+), 2 deletions(-) diff --git a/db/services.php b/db/services.php index 95a0ccd..cf15b3c 100644 --- a/db/services.php +++ b/db/services.php @@ -131,8 +131,88 @@ 'type' => 'write', 'ajax' => true, 'capabilities' => 'mod/label:addinstance', + ), + 'local_course_update_course_module_page' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_update_course_module_page', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Update course module of type page', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/page:addinstance', ), - + 'local_course_update_course_module_assignment' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_update_course_module_assignment', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Update course module of type assignment', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/assign:addinstance', + ), + 'local_course_update_course_module_lesson' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_update_course_module_lesson', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Update course module of type lesson', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/lesson:addinstance', + ), + 'local_course_update_course_module_lesson_contentpage' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_update_course_module_lesson_contentpage', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Update page in a lesson', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/lesson:addinstance', + ), + 'local_course_save_attachment_in_assignment' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_assignment_save_attachment', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Save attachment in assignment module', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/assign:addinstance', + ), + 'local_course_save_attachment_in_label' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_label_save_attachment', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Add attachment in label module', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/label:addinstance', + ), + 'local_course_save_attachment_in_page' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_page_save_attachment', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Add attachment in page module', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/page:addinstance', + ), + 'local_course_save_attachment_in_lesson' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_lesson_save_attachment', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Save attachment in Lesson module', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/lesson:addinstance', + ), + 'local_course_save_attachment_in_lessonpage' => array( + 'classname' => 'local_sync_service_external', + 'methodname' => 'local_sync_service_lessonpage_save_attachment', + 'classpath' => 'local/sync_service/externallib.php', + 'description' => 'Save attachment in Lesson module', + 'type' => 'write', + 'ajax' => true, + 'capabilities' => 'mod/lesson:addinstance', + ) ); @@ -152,6 +232,15 @@ 'local_course_move_module_to_specific_position', 'local_course_add_new_course_module_directory', 'local_course_add_files_to_directory', + 'local_course_update_course_module_page', + 'local_course_update_course_module_assignment', + 'local_course_update_course_module_lesson', + 'local_course_update_course_module_lesson_contentpage', + 'local_course_save_attachment_in_assignment', + 'local_course_save_attachment_in_label', + 'local_course_save_attachment_in_page', + 'local_course_save_attachment_in_lesson', + 'local_course_save_attachment_in_lessonpage', 'core_course_get_courses', 'core_course_get_contents', 'core_course_get_course_module', diff --git a/externallib.php b/externallib.php index 75c1cd2..f802d06 100644 --- a/externallib.php +++ b/externallib.php @@ -1145,5 +1145,889 @@ public static function local_sync_service_update_course_module_label_returns() { } + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + public static function local_sync_service_update_course_module_page_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value( PARAM_TEXT, 'id of module' ), + 'content' => new external_value( PARAM_TEXT, 'HTML or Markdown code' ), + 'format' => new external_value( PARAM_TEXT, 'Markdown or HTML', VALUE_DEFAULT, \FORMAT_MARKDOWN ), + ) + ); + } + + /** + * Method to update a new course module containing a file. + * + * @param $cmid The course module id. + * @param $content Content to add + * @param $format HTML or Markdown(=default) + * @return $update Message: Successful and $cmid of the new Module. + */ + public static function local_sync_service_update_course_module_page($cmid, $content, $format) { + global $DB, $CFG; + require_once($CFG->dirroot . '/mod/' . '/page' . '/lib.php'); + require_once($CFG->dirroot . '/course/' . '/modlib.php'); + + //debug("local_sync_service_update_course_module_page\n"); + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_update_course_module_page_parameters(), + array( + 'cmid' => $cmid, + 'content' => $content, + 'format' => $format + ) + ); + + $cm = get_coursemodule_from_id('page', $cmid, 0, false, MUST_EXIST); + + // Ensure the current user has required permission in this course. + $context = context_module::instance($cmid); + self::validate_context($context); + + // Required permissions. + require_capability('mod/page:addinstance', $context); + + $modulename = 'page'; + $cm->module = $DB->get_field( 'modules', 'id', array('name' => $modulename) ); + $instance = new \stdClass(); + $instance->course = $cm->course; + $instance->contentformat = $format; + $instance->page = [ + 'text' => html_entity_decode($content), + 'format' => $format, + ]; + + $instance->coursemodule = $cmid; + $instance->instance = $cm->instance; + $instance->modulename = $modulename; + $instance->type = 'mod'; + $instance->visible = true; + $instance->id = page_update_instance($instance, null); + + $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); + $update = [ + 'message' => 'Successful', + 'id' => $cmid, + ]; + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_update_course_module_page_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'id' => new external_value( PARAM_TEXT, 'cmid of the new module' ), + ) + ); + } + + + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + + public static function local_sync_service_update_course_module_assignment_parameters() { + return new external_function_parameters( + array( + 'assignments' => new external_multiple_structure( + new external_single_structure( + array( + 'cmid' => new external_value(PARAM_INT, 'ID of the assignment module'), + 'desc' => new external_value(PARAM_TEXT, 'description of assisngment'), + 'activity' => new external_value(PARAM_TEXT, 'activity in assignment', VALUE_OPTIONAL) + ) + ), 'assignment courses to update' + ) + ) + ); + } + + /** + * Method to update a new course module containing a assignment. + * + * @param $cmid The course module id. + * @param $desc HTML code to add to description + * @return $update Message: Successful and $cmid of the new Module. + */ + public static function local_sync_service_update_course_module_assignment($assignments) { + global $DB, $CFG; + require_once($CFG->dirroot . '/mod/' . '/assign' . '/lib.php'); + require_once($CFG->dirroot . '/mod/' . '/assign' . '/locallib.php'); + $warnings = array(); + + //debug("local_sync_service_update_course_module_assignment\n"); + + $params = self::validate_parameters(self::local_sync_service_update_course_module_assignment_parameters(), array('assignments' => $assignments)); + + foreach ($params['assignments'] as $ass) { + try { + $cmid = $ass['cmid']; + $desc = $ass['desc']; + //debug(" cmid=$cmid , desc=$desc\n"); + + if (array_key_exists('activity', $ass) ) { + $activity = $ass['activity']; + } + + }catch (Exception $e) { + debug(" exception\n"); + $warning = array(); + $warning['item'] = 'assignments'; + $warning['itemid'] = $ass['cmid']; + if ($e instanceof moodle_exception) { + $warning['warningcode'] = $e->errorcode; + } else { + $warning['warningcode'] = $e->getCode(); + } + $warning['message'] = $e->getMessage(); + $warnings[] = $warning; + } + } + + $cm = get_coursemodule_from_id('assign', $cmid, 0, false, MUST_EXIST); + // Ensure the current user has required permission in this course. + $context = context_module::instance($cmid); + self::validate_context($context); + require_capability('mod/assign:addinstance', $context); + + $dbparams = array('id'=>$cm->instance); + if (! $instance = $DB->get_record('assign', $dbparams, '*')) { + return false; + } + + $instance->id = $cm->instance; + + $instance->activityeditor = [ + 'text' => html_entity_decode($activity), + 'format' => \FORMAT_MARKDOWN, + ]; + + $instance->introformat = \FORMAT_MARKDOWN; + $instance->activityformat = \FORMAT_MARKDOWN; + $instance->coursemodule = $cmid; + $instance->instance = $cm->instance; + $instance->modulename ='assign'; + $instance->type = 'mod'; + $instance->visible = true; + + $instance->intro = html_entity_decode($desc); + $instance->id = assign_update_instance($instance, null); + + $update = [ + 'message' => 'Successful', + 'id' => $cmid, + ]; + + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_update_course_module_assignment_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'id' => new external_value( PARAM_TEXT, 'cmid of the new module' ), + ) + ); + } + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + + public static function local_sync_service_update_course_module_lesson_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value( PARAM_INT, 'id of module' ), + 'desc' => new external_value( PARAM_TEXT, 'description' ) + ) + ); + } + + /** + * Method to update a lesson module + * + * @param $cmid The course module id. + * @param $desc content to add to description + * @return $update Message: Successful and $cmid of the new Module. + */ + public static function local_sync_service_update_course_module_lesson($cmid, $desc) { + global $DB, $CFG; + //debug("local_sync_service_update_course_module_lesson"); + + require_once($CFG->dirroot . '/mod/' . '/lesson' . '/lib.php'); + require_once($CFG->dirroot . '/mod/' . '/lesson' . '/locallib.php'); + + + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_update_course_module_lesson_parameters(), + array( + 'cmid' => $cmid, + 'desc' => $desc + ) + ); + + + $cm = get_coursemodule_from_id('lesson', $cmid, 0, false, MUST_EXIST); + $instance = $DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST); + $context = context_module::instance($cmid); + self::validate_context($context); + require_capability('mod/lesson:addinstance', $context); + + $instance->intro=html_entity_decode($desc); + $instance->introformat = \FORMAT_MARKDOWN; + $instance->coursemodule = $cmid; + $instance->instance = $cm->instance; + $instance->modulename ='lesson'; + $instance->type = 'mod'; + $instance->visible = true; + + $instance->id = lesson_update_instance($instance, null); + + $update = [ + 'message' => 'Successful', + 'id' => $cmid, + ]; + + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_update_course_module_lesson_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'id' => new external_value( PARAM_TEXT, 'cmid of the new module' ), + ) + ); + } + + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + + public static function local_sync_service_update_course_module_lesson_contentpage_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value( PARAM_INT, 'id of module' ), + 'pageid' => new external_value( PARAM_INT, 'pageid of lesson content' ), + 'title' => new external_value( PARAM_TEXT, 'title of lesson content page',VALUE_OPTIONAL ), + 'content' => new external_value( PARAM_TEXT, 'HTML or Markdown code' ), + + ) + ); + } + + /** + * Method to update a lesson module and add a content page to it. + * + * @param $cmid The cours $title,e module id. + * @param $desc content to add to description + * @return $update Message: Successful and $cmid of the new Module. + */ + public static function local_sync_service_update_course_module_lesson_contentpage($cmid, $pageid, $title, $content) { + global $DB, $CFG; + require_once($CFG->dirroot . '/mod/' . '/lesson' . '/lib.php'); + require_once($CFG->dirroot . '/mod/' . '/lesson' . '/locallib.php'); + $warnings = array(); + + //debug("local_sync_service_update_course_module_lesson_contentpage\n"); + + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_update_course_module_lesson_contentpage_parameters(), + array( + 'cmid' => $cmid, + 'pageid' => $pageid, + 'title' => $title, + 'content' => $content + ) + ); + $cm = get_coursemodule_from_id('lesson', $cmid, 0, false, MUST_EXIST); + $instance = $DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST); + $context = context_module::instance($cmid); + self::validate_context($context); + require_capability('mod/lesson:addinstance', $context); + + $lesson = new Lesson($instance); + $page = $lesson->load_page($pageid); + $prop = $page->properties(); + $prop->contents=html_entity_decode($content); + if (!empty($title)) { + $prop->title=$title; + } + $DB->update_record("lesson_pages", $prop); + + $update = [ + 'message' => 'Successful', + 'id' => $cmid, + ]; + + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_update_course_module_lesson_contentpage_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'id' => new external_value( PARAM_TEXT, 'cmid of the new module' ), + ) + ); + } + + + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + public static function local_sync_service_assignment_save_attachment_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value( PARAM_TEXT, 'id of module' ), + 'itemid' => new external_value( PARAM_TEXT, 'id of draft area where file uploaded' ), + 'filename' => new external_value( PARAM_TEXT, 'filename' ) + ) + ); + } + + /** + * Method to update a new course module containing a file. + * + * @param $courseid The course id. + * @param $itemid File to publish. + * @return $update Message: Successful and $cmid of the new Module. + */ + public static function local_sync_service_assignment_save_attachment($cmid, $itemid, $filename) { + global $DB, $CFG,$USER; + + require_once($CFG->dirroot . '/mod/' . '/assign' . '/lib.php'); + require_once($CFG->dirroot . '/mod/' . '/assign' . '/locallib.php'); + + //debug("local_sync_service_assignment_save_attachment\n"); + + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_assignment_save_attachment_parameters(), + array( + 'cmid' => $cmid, + 'itemid' => $itemid, + 'filename' => $filename + ) + ); + + $cm = get_coursemodule_from_id('assign', $cmid, 0, false, MUST_EXIST); + $instance = $DB->get_record('assign', array('id' => $cm->instance), '*', MUST_EXIST); + + // Ensure the current user has required permission in this course. + $context = context_module::instance($cmid); + self::validate_context($context); + + // Required permissions. + require_capability('mod/assign:addinstance', $context); + require_capability('moodle/course:managefiles', $context); + + $fs = get_file_storage(); + $usercontext = \context_user::instance($USER->id); + + $files = $fs->get_area_files($context->id, 'mod_assign', 'intro'/*ASSIGN_INTROATTACHMENT_FILEAREA*/, 0); + foreach ($files as $file) { + if ($file->get_filename() == $filename /*and $file->get_itemid() == $itemid*/) { + $file->delete(); + } + } + + $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid); + + foreach ($files as $file) { + + $fileinfo = [ + 'contextid' => $context->id, // ID of the context. + 'component' => 'mod_assign', // Your component name. + 'filearea' => 'intro', //ASSIGN_INTROATTACHMENT_FILEAREA, // Usually = table name. + 'itemid' => 0, // Usually = ID of row in table. + 'filepath' => '/', // Any path beginning and ending in /. + 'filename' => $file->get_filename(), // Any filename. + ]; + + if ($file->get_filename() == $filename /*and $file->get_itemid() == $itemid*/ ) { + $fs->create_file_from_storedfile($fileinfo, $file); + + $url = moodle_url::make_draftfile_url( + $file->get_itemid(), + $file->get_filepath(), + $file->get_filename(), + false + ); + + break; + } + + } + + $instance->coursemodule = $cmid; + $instance->instance = $cm->instance; + $instance->id = $cm->instance; + $instance->id = assign_update_instance($instance, null); + + $update = [ + 'message' => 'Successful', + 'id' => $cmid, + ]; + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_assignment_save_attachment_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'id' => new external_value( PARAM_TEXT, 'cmid of the new module' ), + //'url' => new external_value( PARAM_TEXT, 'url of the uploaded itemid' ), + ) + ); + } + + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + public static function local_sync_service_label_save_attachment_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value( PARAM_TEXT, 'id of module' ), + 'itemid' => new external_value( PARAM_TEXT, 'id of draft area where file uploaded' ), + 'filename' => new external_value( PARAM_TEXT, 'filename' ) + ) + ); + } + + /** + * + * @param $cmdid The id of the label module + * @param $itemid File to publish. + * @return $update Message: Successful and $cmid of the new Module. + */ + public static function local_sync_service_label_save_attachment($cmid, $itemid, $filename) { + global $DB, $CFG,$USER; + + require_once($CFG->dirroot . '/mod/' . '/label' . '/lib.php'); + + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_label_save_attachment_parameters(), + array( + 'cmid' => $cmid, + 'itemid' => $itemid, + 'filename' => $filename + ) + ); + + //debug(" cmid=$cmid , itemid=$itemid, filename=$filename\n"); + + $cm = get_coursemodule_from_id('label', $cmid, 0, false, MUST_EXIST); + $instance = $DB->get_record('label', array('id' => $cm->instance), '*', MUST_EXIST); + $context = context_module::instance($cmid); + self::validate_context($context); + + // Required permissions. + require_capability('mod/label:addinstance', $context); + require_capability('moodle/course:managefiles', $context); + + $fs = get_file_storage(); + $usercontext = \context_user::instance($USER->id); + $files = $fs->get_area_files($context->id, 'mod_label', 'intro', 0); + foreach ($files as $file) { + if ($file->get_filename() == $filename) { + $file->delete(); + } + + } + + $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid); + foreach ($files as $file) { + $fileinfo = [ + 'contextid' => $context->id, // ID of the context. + 'component' => 'mod_label', // Your component name. + 'filearea' => 'intro', + 'itemid' => 0, // Usually = ID of row in table. + 'filepath' => '/', // Any path beginning and ending in /. + 'filename' => $file->get_filename(), // Any filename. + ]; + + if ($file->get_filename() == $filename ) { + // debug("create store file for $filename ($itemid)\n"); + $fs->create_file_from_storedfile($fileinfo, $file); + + $url = moodle_url::make_draftfile_url( + $file->get_itemid(), + $file->get_filepath(), + $file->get_filename(), + false + ); + // debug("Draft URL: $url\n"); + break; + } + + } + + $instance->coursemodule = $cmid; + $instance->instance = $cm->instance; + $instance->id = $cm->instance; + $instance->id = label_update_instance($instance); + + + $update = [ + 'message' => 'Successful', + 'id' => $cmid, + ]; + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_label_save_attachment_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'id' => new external_value( PARAM_TEXT, 'cmid of the new module' ), + ) + ); + } + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + public static function local_sync_service_page_save_attachment_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value( PARAM_TEXT, 'id of module' ), + 'itemid' => new external_value( PARAM_TEXT, 'id of draft area where file uploaded' ), + 'filename' => new external_value( PARAM_TEXT, 'filename' ) + ) + ); + } + + /** + * + * @param $cmdid The id of the label module + * @param $itemid File to publish. + * @return $update Message: Successful and $cmid of the new Module. + */ + public static function local_sync_service_page_save_attachment($cmid, $itemid, $filename) { + global $DB, $CFG,$USER; + + require_once($CFG->dirroot . '/mod/' . '/page' . '/lib.php'); + + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_page_save_attachment_parameters(), + array( + 'cmid' => $cmid, + 'itemid' => $itemid, + 'filename' => $filename + ) + ); + + $cm = get_coursemodule_from_id('page', $cmid, 0, false, MUST_EXIST); + $instance = $DB->get_record('page', array('id' => $cm->instance), '*', MUST_EXIST); + $context = context_module::instance($cmid); + self::validate_context($context); + + // Required permissions. + require_capability('mod/page:addinstance', $context); + require_capability('moodle/course:managefiles', $context); + + $fs = get_file_storage(); + $usercontext = \context_user::instance($USER->id); + $files = $fs->get_area_files($context->id, 'mod_page', 'content', 0); + foreach ($files as $file) { + if ($file->get_filename() == $filename) { + $file->delete(); + } + } + + $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid); + foreach ($files as $file) { + + $fileinfo = [ + 'contextid' => $context->id, // ID of the context. + 'component' => 'mod_page', // Your component name. + 'filearea' => 'content', + 'itemid' => 0, // Usually = ID of row in table. + 'filepath' => '/', // Any path beginning and ending in /. + 'filename' => $file->get_filename(), // Any filename. + ]; + + if ($file->get_filename() == $filename ) { + $fs->create_file_from_storedfile($fileinfo, $file); + break; + } + } + + $update = [ + 'message' => 'Successful', + 'id' => $cmid, + ]; + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_page_save_attachment_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'id' => new external_value( PARAM_TEXT, 'cmid of the new module' ), + ) + ); + } + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + public static function local_sync_service_lesson_save_attachment_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value( PARAM_TEXT, 'id of module' ), + 'itemid' => new external_value( PARAM_TEXT, 'id of draft area where file uploaded' ), + 'filename' => new external_value( PARAM_TEXT, 'filename' ) + ) + ); + } + + + /** + * + * @param $cmdid The id of the label module + * @param $itemid File to publish. + * @return $update Message: Successful and $cmid of the new Module. + */ + public static function local_sync_service_lesson_save_attachment($cmid, $itemid, $filename) { + global $DB, $CFG,$USER; + + require_once($CFG->dirroot . '/mod/' . '/lesson' . '/lib.php'); + + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_lesson_save_attachment_parameters(), + array( + 'cmid' => $cmid, + 'itemid' => $itemid, + 'filename' => $filename + ) + ); + + $cm = get_coursemodule_from_id('lesson', $cmid, 0, false, MUST_EXIST); + $instance = $DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST); + $context = context_module::instance($cmid); + self::validate_context($context); + + // Required permissions. + require_capability('mod/lesson:addinstance', $context); + require_capability('moodle/course:managefiles', $context); + + $fs = get_file_storage(); + $usercontext = \context_user::instance($USER->id); + $files = $fs->get_area_files($context->id, 'mod_lesson', 'intro', 0); + foreach ($files as $file) { + if ($file->get_filename() == $filename) { + $file->delete(); + } + + } + + $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid); + foreach ($files as $file) { + $fileinfo = [ + 'contextid' => $context->id, // ID of the context. + 'component' => 'mod_lesson', // Your component name. + 'filearea' => 'intro', + 'itemid' => 0, // Usually = ID of row in table. + 'filepath' => '/', // Any path beginning and ending in /. + 'filename' => $file->get_filename(), // Any filename. + ]; + + if ($file->get_filename() == $filename ) { + $fs->create_file_from_storedfile($fileinfo, $file); + + $url = moodle_url::make_draftfile_url( + $file->get_itemid(), + $file->get_filepath(), + $file->get_filename(), + false + ); + break; + } + + } + + $instance->coursemodule = $cmid; + $instance->instance = $cm->instance; + $instance->id = $cm->instance; + $instance->id = lesson_update_instance($instance,null); + $update = [ + 'message' => 'Successful', + 'id' => $cmid, + ]; + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_lesson_save_attachment_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'id' => new external_value( PARAM_TEXT, 'cmid of the new module' ), + ) + ); + } + + + + /** + * Defines the necessary method parameters. + * @return external_function_parameters + */ + public static function local_sync_service_lessonpage_save_attachment_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value( PARAM_TEXT, 'id of module' ), + 'itemid' => new external_value( PARAM_TEXT, 'id of draft area where file uploaded' ), + 'pageid' => new external_value( PARAM_TEXT, 'pageid of lesson content page' ), + 'filename' => new external_value( PARAM_TEXT, 'filename' ) + ) + ); + } + + + /** + * + * @param $cmdid The id of the lesson module + * @param $itemid File to publish. + * @return $update Message: Successful and $cmid of the new Module. + */ + public static function local_sync_service_lessonpage_save_attachment($cmid, $itemid, $pageid, $filename) { + global $DB, $CFG,$USER; + + require_once($CFG->dirroot . '/mod/' . '/lesson' . '/lib.php'); + + // Parameter validation. + $params = self::validate_parameters( + self::local_sync_service_lessonpage_save_attachment_parameters(), + array( + 'cmid' => $cmid, + 'itemid' => $itemid, + 'pageid' => $pageid, + 'filename' => $filename + ) + ); + + $cm = get_coursemodule_from_id('lesson', $cmid, 0, false, MUST_EXIST); + $instance = $DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST); + $context = context_module::instance($cmid); + self::validate_context($context); + + // Required permissions. + require_capability('mod/lesson:addinstance', $context); + require_capability('moodle/course:managefiles', $context); + + $fs = get_file_storage(); + $usercontext = \context_user::instance($USER->id); + $files = $fs->get_area_files($context->id, 'mod_lesson', 'page_contents', $pageid); + foreach ($files as $file) { + if ($file->get_filename() == $filename) { + $file->delete(); + } + } + + $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid); + foreach ($files as $file) { + + $fileinfo = [ + 'contextid' => $context->id, // ID of the context. + 'component' => 'mod_lesson', // Your component name. + 'filearea' => 'page_contents', + 'itemid' => $pageid, // ID of row in table mdl_lesson_page + 'filepath' => '/', // Any path beginning and ending in /. + 'filename' => $file->get_filename(), // Any filename. + ]; + + if ($file->get_filename() == $filename ) { + $fs->create_file_from_storedfile($fileinfo, $file); + + $url = moodle_url::make_draftfile_url( + $file->get_itemid(), + $file->get_filepath(), + $file->get_filename(), + false + ); + break; + } + + } + + $instance->coursemodule = $cmid; + $instance->instance = $cm->instance; + $instance->id = $cm->instance; + $instance->id = lesson_update_instance($instance,null); + + $update = [ + 'message' => 'Successful', + 'id' => $cmid, + ]; + return $update; + } + + /** + * Obtains the Parameter which will be returned. + * @return external_description + */ + public static function local_sync_service_lessonpage_save_attachment_returns() { + return new external_single_structure( + array( + 'message' => new external_value( PARAM_TEXT, 'if the execution was successful' ), + 'id' => new external_value( PARAM_TEXT, 'cmid of the new module' ), + ) + ); + } } diff --git a/version.php b/version.php index 3c278bf..5a19e7d 100644 --- a/version.php +++ b/version.php @@ -23,5 +23,5 @@ */ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'local_sync_service'; -$plugin->version = 2023062900; +$plugin->version = 2024070800; $plugin->requires = 2021051704.00;