2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
21 * @package core_course
23 * @copyright 2009 Petr Skodak
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die;
29 require_once("$CFG->libdir/externallib.php");
32 * Course external functions
34 * @package core_course
36 * @copyright 2011 Jerome Mouneyrac
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class core_course_external
extends external_api
{
43 * Returns description of method parameters
45 * @return external_function_parameters
48 public static function get_course_contents_parameters() {
49 return new external_function_parameters(
50 array('courseid' => new external_value(PARAM_INT
, 'course id'),
51 'options' => new external_multiple_structure (
52 new external_single_structure(
53 array('name' => new external_value(PARAM_ALPHANUM
, 'option name'),
54 'value' => new external_value(PARAM_RAW
, 'the value of the option, this param is personaly validated in the external function.')
56 ), 'Options, not used yet, might be used in later version', VALUE_DEFAULT
, array())
64 * @param int $courseid course id
65 * @param array $options These options are not used yet, might be used in later version
69 public static function get_course_contents($courseid, $options) {
71 require_once($CFG->dirroot
. "/course/lib.php");
74 $params = self
::validate_parameters(self
::get_course_contents_parameters(),
75 array('courseid' => $courseid, 'options' => $options));
78 $course = $DB->get_record('course', array('id' => $params['courseid']), '*', MUST_EXIST
);
80 //check course format exist
81 if (!file_exists($CFG->dirroot
. '/course/format/' . $course->format
. '/lib.php')) {
82 throw new moodle_exception('cannotgetcoursecontents', 'webservice', '', null, get_string('courseformatnotfound', 'error', '', $course->format
));
84 require_once($CFG->dirroot
. '/course/format/' . $course->format
. '/lib.php');
87 // now security checks
88 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
90 self
::validate_context($context);
91 } catch (Exception
$e) {
92 $exceptionparam = new stdClass();
93 $exceptionparam->message
= $e->getMessage();
94 $exceptionparam->courseid
= $course->id
;
95 throw new moodle_exception('errorcoursecontextnotvalid', 'webservice', '', $exceptionparam);
98 $canupdatecourse = has_capability('moodle/course:update', $context);
100 //create return value
101 $coursecontents = array();
103 if ($canupdatecourse or $course->visible
104 or has_capability('moodle/course:viewhiddencourses', $context)) {
107 $modinfo = get_fast_modinfo($course);
108 $sections = get_all_sections($course->id
);
110 //for each sections (first displayed to last displayed)
111 foreach ($sections as $key => $section) {
113 $showsection = (has_capability('moodle/course:viewhiddensections', $context) or $section->visible
or !$course->hiddensections
);
118 // reset $sectioncontents
119 $sectionvalues = array();
120 $sectionvalues['id'] = $section->id
;
121 $sectionvalues['name'] = get_section_name($course, $section);
122 $summary = file_rewrite_pluginfile_urls($section->summary
, 'webservice/pluginfile.php', $context->id
, 'course', 'section', $section->id
);
123 $sectionvalues['visible'] = $section->visible
;
124 $sectionvalues['summary'] = format_text($summary, $section->summaryformat
);
125 $sectioncontents = array();
127 //for each module of the section
128 foreach ($modinfo->sections
[$section->section
] as $cmid) { //matching /course/lib.php:print_section() logic
129 $cm = $modinfo->cms
[$cmid];
131 // stop here if the module is not visible to the user
132 if (!$cm->uservisible
) {
138 //common info (for people being able to see the module or availability dates)
139 $module['id'] = $cm->id
;
140 $module['name'] = format_string($cm->name
, true);
141 $module['modname'] = $cm->modname
;
142 $module['modplural'] = $cm->modplural
;
143 $module['modicon'] = $cm->get_icon_url()->out(false);
144 $module['indent'] = $cm->indent
;
146 $modcontext = get_context_instance(CONTEXT_MODULE
, $cm->id
);
148 if (!empty($cm->showdescription
)) {
149 $module['description'] = $cm->get_content();
153 $url = $cm->get_url();
154 if ($url) { //labels don't have url
155 $module['url'] = $cm->get_url()->out();
158 $canviewhidden = has_capability('moodle/course:viewhiddenactivities',
159 get_context_instance(CONTEXT_MODULE
, $cm->id
));
160 //user that can view hidden module should know about the visibility
161 $module['visible'] = $cm->visible
;
163 //availability date (also send to user who can see hidden module when the showavailabilyt is ON)
164 if ($canupdatecourse or ($CFG->enableavailability
&& $canviewhidden && $cm->showavailability
)) {
165 $module['availablefrom'] = $cm->availablefrom
;
166 $module['availableuntil'] = $cm->availableuntil
;
169 $baseurl = 'webservice/pluginfile.php';
171 //call $modulename_export_contents
172 //(each module callback take care about checking the capabilities)
173 require_once($CFG->dirroot
. '/mod/' . $cm->modname
. '/lib.php');
174 $getcontentfunction = $cm->modname
.'_export_contents';
175 if (function_exists($getcontentfunction)) {
176 if ($contents = $getcontentfunction($cm, $baseurl)) {
177 $module['contents'] = $contents;
181 //assign result to $sectioncontents
182 $sectioncontents[] = $module;
185 $sectionvalues['modules'] = $sectioncontents;
187 // assign result to $coursecontents
188 $coursecontents[] = $sectionvalues;
191 return $coursecontents;
195 * Returns description of method result value
197 * @return external_description
200 public static function get_course_contents_returns() {
201 return new external_multiple_structure(
202 new external_single_structure(
204 'id' => new external_value(PARAM_INT
, 'Section ID'),
205 'name' => new external_value(PARAM_TEXT
, 'Section name'),
206 'visible' => new external_value(PARAM_INT
, 'is the section visible', VALUE_OPTIONAL
),
207 'summary' => new external_value(PARAM_RAW
, 'Section description'),
208 'modules' => new external_multiple_structure(
209 new external_single_structure(
211 'id' => new external_value(PARAM_INT
, 'activity id'),
212 'url' => new external_value(PARAM_URL
, 'activity url', VALUE_OPTIONAL
),
213 'name' => new external_value(PARAM_TEXT
, 'activity module name'),
214 'description' => new external_value(PARAM_RAW
, 'activity description', VALUE_OPTIONAL
),
215 'visible' => new external_value(PARAM_INT
, 'is the module visible', VALUE_OPTIONAL
),
216 'modicon' => new external_value(PARAM_URL
, 'activity icon url'),
217 'modname' => new external_value(PARAM_PLUGIN
, 'activity module type'),
218 'modplural' => new external_value(PARAM_TEXT
, 'activity module plural name'),
219 'availablefrom' => new external_value(PARAM_INT
, 'module availability start date', VALUE_OPTIONAL
),
220 'availableuntil' => new external_value(PARAM_INT
, 'module availability en date', VALUE_OPTIONAL
),
221 'indent' => new external_value(PARAM_INT
, 'number of identation in the site'),
222 'contents' => new external_multiple_structure(
223 new external_single_structure(
226 'type'=> new external_value(PARAM_TEXT
, 'a file or a folder or external link'),
227 'filename'=> new external_value(PARAM_FILE
, 'filename'),
228 'filepath'=> new external_value(PARAM_PATH
, 'filepath'),
229 'filesize'=> new external_value(PARAM_INT
, 'filesize'),
230 'fileurl' => new external_value(PARAM_URL
, 'downloadable file url', VALUE_OPTIONAL
),
231 'content' => new external_value(PARAM_RAW
, 'Raw content, will be used when type is content', VALUE_OPTIONAL
),
232 'timecreated' => new external_value(PARAM_INT
, 'Time created'),
233 'timemodified' => new external_value(PARAM_INT
, 'Time modified'),
234 'sortorder' => new external_value(PARAM_INT
, 'Content sort order'),
236 // copyright related info
237 'userid' => new external_value(PARAM_INT
, 'User who added this content to moodle'),
238 'author' => new external_value(PARAM_TEXT
, 'Content owner'),
239 'license' => new external_value(PARAM_TEXT
, 'Content license'),
241 ), VALUE_DEFAULT
, array()
252 * Returns description of method parameters
254 * @return external_function_parameters
257 public static function get_courses_parameters() {
258 return new external_function_parameters(
259 array('options' => new external_single_structure(
260 array('ids' => new external_multiple_structure(
261 new external_value(PARAM_INT
, 'Course id')
262 , 'List of course id. If empty return all courses
263 except front page course.',
265 ), 'options - operator OR is used', VALUE_DEFAULT
, array())
273 * @param array $options It contains an array (list of ids)
277 public static function get_courses($options) {
279 require_once($CFG->dirroot
. "/course/lib.php");
282 $params = self
::validate_parameters(self
::get_courses_parameters(),
283 array('options' => $options));
286 if (!key_exists('ids', $params['options'])
287 or empty($params['options']['ids'])) {
288 $courses = $DB->get_records('course');
290 $courses = $DB->get_records_list('course', 'id', $params['options']['ids']);
293 //create return value
294 $coursesinfo = array();
295 foreach ($courses as $course) {
297 // now security checks
298 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
300 self
::validate_context($context);
301 } catch (Exception
$e) {
302 $exceptionparam = new stdClass();
303 $exceptionparam->message
= $e->getMessage();
304 $exceptionparam->courseid
= $course->id
;
305 throw new moodle_exception(
306 get_string('errorcoursecontextnotvalid', 'webservice', $exceptionparam));
308 require_capability('moodle/course:view', $context);
310 $courseinfo = array();
311 $courseinfo['id'] = $course->id
;
312 $courseinfo['fullname'] = $course->fullname
;
313 $courseinfo['shortname'] = $course->shortname
;
314 $courseinfo['categoryid'] = $course->category
;
315 $courseinfo['summary'] = $course->summary
;
316 $courseinfo['summaryformat'] = $course->summaryformat
;
317 $courseinfo['format'] = $course->format
;
318 $courseinfo['startdate'] = $course->startdate
;
319 $courseinfo['numsections'] = $course->numsections
;
321 //some field should be returned only if the user has update permission
322 $courseadmin = has_capability('moodle/course:update', $context);
324 $courseinfo['categorysortorder'] = $course->sortorder
;
325 $courseinfo['idnumber'] = $course->idnumber
;
326 $courseinfo['showgrades'] = $course->showgrades
;
327 $courseinfo['showreports'] = $course->showreports
;
328 $courseinfo['newsitems'] = $course->newsitems
;
329 $courseinfo['visible'] = $course->visible
;
330 $courseinfo['maxbytes'] = $course->maxbytes
;
331 $courseinfo['hiddensections'] = $course->hiddensections
;
332 $courseinfo['groupmode'] = $course->groupmode
;
333 $courseinfo['groupmodeforce'] = $course->groupmodeforce
;
334 $courseinfo['defaultgroupingid'] = $course->defaultgroupingid
;
335 $courseinfo['lang'] = $course->lang
;
336 $courseinfo['timecreated'] = $course->timecreated
;
337 $courseinfo['timemodified'] = $course->timemodified
;
338 $courseinfo['forcetheme'] = $course->theme
;
339 $courseinfo['enablecompletion'] = $course->enablecompletion
;
340 $courseinfo['completionstartonenrol'] = $course->completionstartonenrol
;
341 $courseinfo['completionnotify'] = $course->completionnotify
;
344 if ($courseadmin or $course->visible
345 or has_capability('moodle/course:viewhiddencourses', $context)) {
346 $coursesinfo[] = $courseinfo;
354 * Returns description of method result value
356 * @return external_description
359 public static function get_courses_returns() {
360 return new external_multiple_structure(
361 new external_single_structure(
363 'id' => new external_value(PARAM_INT
, 'course id'),
364 'shortname' => new external_value(PARAM_TEXT
, 'course short name'),
365 'categoryid' => new external_value(PARAM_INT
, 'category id'),
366 'categorysortorder' => new external_value(PARAM_INT
,
367 'sort order into the category', VALUE_OPTIONAL
),
368 'fullname' => new external_value(PARAM_TEXT
, 'full name'),
369 'idnumber' => new external_value(PARAM_RAW
, 'id number', VALUE_OPTIONAL
),
370 'summary' => new external_value(PARAM_RAW
, 'summary'),
371 'summaryformat' => new external_value(PARAM_INT
,
372 'the summary text Moodle format'),
373 'format' => new external_value(PARAM_PLUGIN
,
374 'course format: weeks, topics, social, site,..'),
375 'showgrades' => new external_value(PARAM_INT
,
376 '1 if grades are shown, otherwise 0', VALUE_OPTIONAL
),
377 'newsitems' => new external_value(PARAM_INT
,
378 'number of recent items appearing on the course page', VALUE_OPTIONAL
),
379 'startdate' => new external_value(PARAM_INT
,
380 'timestamp when the course start'),
381 'numsections' => new external_value(PARAM_INT
, 'number of weeks/topics'),
382 'maxbytes' => new external_value(PARAM_INT
,
383 'largest size of file that can be uploaded into the course',
385 'showreports' => new external_value(PARAM_INT
,
386 'are activity report shown (yes = 1, no =0)', VALUE_OPTIONAL
),
387 'visible' => new external_value(PARAM_INT
,
388 '1: available to student, 0:not available', VALUE_OPTIONAL
),
389 'hiddensections' => new external_value(PARAM_INT
,
390 'How the hidden sections in the course are displayed to students',
392 'groupmode' => new external_value(PARAM_INT
, 'no group, separate, visible',
394 'groupmodeforce' => new external_value(PARAM_INT
, '1: yes, 0: no',
396 'defaultgroupingid' => new external_value(PARAM_INT
, 'default grouping id',
398 'timecreated' => new external_value(PARAM_INT
,
399 'timestamp when the course have been created', VALUE_OPTIONAL
),
400 'timemodified' => new external_value(PARAM_INT
,
401 'timestamp when the course have been modified', VALUE_OPTIONAL
),
402 'enablecompletion' => new external_value(PARAM_INT
,
403 'Enabled, control via completion and activity settings. Disbaled,
404 not shown in activity settings.',
406 'completionstartonenrol' => new external_value(PARAM_INT
,
407 '1: begin tracking a student\'s progress in course completion
408 after course enrolment. 0: does not',
410 'completionnotify' => new external_value(PARAM_INT
,
411 '1: yes 0: no', VALUE_OPTIONAL
),
412 'lang' => new external_value(PARAM_SAFEDIR
,
413 'forced course language', VALUE_OPTIONAL
),
414 'forcetheme' => new external_value(PARAM_PLUGIN
,
415 'name of the force theme', VALUE_OPTIONAL
),
422 * Returns description of method parameters
424 * @return external_function_parameters
426 * TODO is PARAM_CLEANHTML for the description enough secured?
427 * Is it possible to still pass some script tag that pass PARAM_CLEANHTML
428 * and execute dangereous javascript in the browser?
430 public static function create_categories_parameters() {
431 return new external_function_parameters(
433 'categories' => new external_multiple_structure(
434 new external_single_structure(
436 'name' => new external_value(PARAM_TEXT
, 'new category name'),
437 'parent' => new external_value(PARAM_INT
, 'the parent category id inside which the new category will be created'),
438 'idnumber' => new external_value(PARAM_RAW
, 'the new category idnumber', VALUE_OPTIONAL
),
439 'description' => new external_value(PARAM_CLEANHTML
, 'the new category description', VALUE_OPTIONAL
),
440 'theme' => new external_value(PARAM_THEME
,
441 'the new category theme. This option must be enabled on moodle', VALUE_OPTIONAL
),
452 * @param array $categories - see create_categories_parameters() for the array structure
453 * @return array - see create_categories_returns() for the array structure
455 * TODO: check exceptions
457 public static function create_categories($categories) {
459 require_once($CFG->dirroot
. "/course/lib.php");
461 $params = self
::validate_parameters(self
::create_categories_parameters(),
462 array('categories' => $categories));
464 $transaction = $DB->start_delegated_transaction();
466 $createdcategories = array();
467 foreach($params['categories'] as $category) {
469 $newcategory = new stdClass();
470 $newcategory->sortorder
= 999; //same as in the course/editcategory.php
471 $newcategory->parent
= $category['parent'];
473 if ($category['parent']) {
474 if (!$DB->record_exists('course_categories', array('id' => $category['parent']))) {
475 throw new moodle_exception('unknowncategory');
477 $context = context_coursecat
::instance($category['parent']);
480 $context = context_system
::instance();
482 self
::validate_context($context);
483 require_capability('moodle/category:manage', $context);
486 if (!empty($category['idnumber'])) { //same as in course/editcategory_form.php
487 if (strlen($category['idnumber'])>100) {
488 throw new moodle_exception('id number is too long');
491 if ($existing = $DB->get_record('course_categories', array('idnumber' => $category['idnumber']))) {
493 throw new moodle_exception('idnumbertaken');
497 $newcategory->idnumber
= $category['idnumber'];
500 if (strlen($category['name'])>30) {
501 throw new moodle_exception('category name is too long.');
503 $newcategory->name
= $category['name'];
505 //Format the description
506 if (!empty($newcategory->description
)) {
507 $options = new stdClass();
508 $options->para
= false;
509 $options->newlines
= false;
510 $options->context
= $context;
511 $newcategory->description
= format_text($category['description'], FORMAT_HTML
, $options);
513 $newcategory->descriptionformat
= FORMAT_HTML
;
515 if (isset($category['theme']) and !empty($CFG->allowcategorythemes
)) {
516 $newcategory->theme
= $category['theme'];
518 $newcategory->id
= $DB->insert_record('course_categories', $newcategory);
519 $newcategory->context
= context_coursecat
::instance($newcategory->id
);
520 mark_context_dirty($newcategory->context
->path
);
522 //populate special fields
523 fix_course_sortorder();
525 $createdcategories[] = array('id' => $newcategory->id
, 'name' => $newcategory->name
);
528 $transaction->allow_commit();
530 return $createdcategories;
534 * Returns description of method parameters
536 * @return external_function_parameters
539 public static function create_categories_returns() {
540 return new external_multiple_structure(
541 new external_single_structure(
543 'id' => new external_value(PARAM_INT
, 'new category id'),
544 'name' => new external_value(PARAM_TEXT
, 'new category name'),
551 * Returns description of method parameters
553 * @return external_function_parameters
556 public static function create_courses_parameters() {
557 $courseconfig = get_config('moodlecourse'); //needed for many default values
558 return new external_function_parameters(
560 'courses' => new external_multiple_structure(
561 new external_single_structure(
563 'fullname' => new external_value(PARAM_TEXT
, 'full name'),
564 'shortname' => new external_value(PARAM_TEXT
, 'course short name'),
565 'categoryid' => new external_value(PARAM_INT
, 'category id'),
566 'idnumber' => new external_value(PARAM_RAW
, 'id number', VALUE_OPTIONAL
),
567 'summary' => new external_value(PARAM_RAW
, 'summary', VALUE_OPTIONAL
),
568 'summaryformat' => new external_value(PARAM_INT
,
569 'the summary text Moodle format', VALUE_DEFAULT
, FORMAT_MOODLE
),
570 'format' => new external_value(PARAM_PLUGIN
,
571 'course format: weeks, topics, social, site,..',
572 VALUE_DEFAULT
, $courseconfig->format
),
573 'showgrades' => new external_value(PARAM_INT
,
574 '1 if grades are shown, otherwise 0', VALUE_DEFAULT
,
575 $courseconfig->showgrades
),
576 'newsitems' => new external_value(PARAM_INT
,
577 'number of recent items appearing on the course page',
578 VALUE_DEFAULT
, $courseconfig->newsitems
),
579 'startdate' => new external_value(PARAM_INT
,
580 'timestamp when the course start', VALUE_OPTIONAL
),
581 'numsections' => new external_value(PARAM_INT
, 'number of weeks/topics',
582 VALUE_DEFAULT
, $courseconfig->numsections
),
583 'maxbytes' => new external_value(PARAM_INT
,
584 'largest size of file that can be uploaded into the course',
585 VALUE_DEFAULT
, $courseconfig->maxbytes
),
586 'showreports' => new external_value(PARAM_INT
,
587 'are activity report shown (yes = 1, no =0)', VALUE_DEFAULT
,
588 $courseconfig->showreports
),
589 'visible' => new external_value(PARAM_INT
,
590 '1: available to student, 0:not available', VALUE_OPTIONAL
),
591 'hiddensections' => new external_value(PARAM_INT
,
592 'How the hidden sections in the course are displayed to students',
593 VALUE_DEFAULT
, $courseconfig->hiddensections
),
594 'groupmode' => new external_value(PARAM_INT
, 'no group, separate, visible',
595 VALUE_DEFAULT
, $courseconfig->groupmode
),
596 'groupmodeforce' => new external_value(PARAM_INT
, '1: yes, 0: no',
597 VALUE_DEFAULT
, $courseconfig->groupmodeforce
),
598 'defaultgroupingid' => new external_value(PARAM_INT
, 'default grouping id',
600 'enablecompletion' => new external_value(PARAM_INT
,
601 'Enabled, control via completion and activity settings. Disabled,
602 not shown in activity settings.',
604 'completionstartonenrol' => new external_value(PARAM_INT
,
605 '1: begin tracking a student\'s progress in course completion after
606 course enrolment. 0: does not',
608 'completionnotify' => new external_value(PARAM_INT
,
609 '1: yes 0: no', VALUE_OPTIONAL
),
610 'lang' => new external_value(PARAM_SAFEDIR
,
611 'forced course language', VALUE_OPTIONAL
),
612 'forcetheme' => new external_value(PARAM_PLUGIN
,
613 'name of the force theme', VALUE_OPTIONAL
),
615 ), 'courses to create'
624 * @param array $courses
625 * @return array courses (id and shortname only)
628 public static function create_courses($courses) {
630 require_once($CFG->dirroot
. "/course/lib.php");
631 require_once($CFG->libdir
. '/completionlib.php');
634 $params = self
::validate_parameters(self
::create_courses_parameters(),
635 array('courses' => $courses));
637 $availablethemes = get_plugin_list('theme');
638 $availablelangs = get_string_manager()->get_list_of_translations();
640 $transaction = $DB->start_delegated_transaction();
642 foreach ($params['courses'] as $course) {
644 // Ensure the current user is allowed to run this function
645 $context = get_context_instance(CONTEXT_COURSECAT
, $course['categoryid']);
647 self
::validate_context($context);
648 } catch (Exception
$e) {
649 $exceptionparam = new stdClass();
650 $exceptionparam->message
= $e->getMessage();
651 $exceptionparam->catid
= $course['categoryid'];
652 throw new moodle_exception(
653 get_string('errorcatcontextnotvalid', 'webservice', $exceptionparam));
655 require_capability('moodle/course:create', $context);
657 // Make sure lang is valid
658 if (key_exists('lang', $course) and empty($availablelangs[$course['lang']])) {
659 throw new moodle_exception(
660 get_string('errorinvalidparam', 'webservice', 'lang'));
663 // Make sure theme is valid
664 if (key_exists('forcetheme', $course)) {
665 if (!empty($CFG->allowcoursethemes
)) {
666 if (empty($availablethemes[$course['forcetheme']])) {
667 throw new moodle_exception(
668 get_string('errorinvalidparam', 'webservice', 'forcetheme'));
670 $course['theme'] = $course['forcetheme'];
675 //force visibility if ws user doesn't have the permission to set it
676 $category = $DB->get_record('course_categories', array('id' => $course['categoryid']));
677 if (!has_capability('moodle/course:visibility', $context)) {
678 $course['visible'] = $category->visible
;
681 //set default value for completion
682 $courseconfig = get_config('moodlecourse');
683 if (completion_info
::is_enabled_for_site()) {
684 if (!key_exists('enablecompletion', $course)) {
685 $course['enablecompletion'] = $courseconfig->enablecompletion
;
687 if (!key_exists('completionstartonenrol', $course)) {
688 $course['completionstartonenrol'] = $courseconfig->completionstartonenrol
;
691 $course['enablecompletion'] = 0;
692 $course['completionstartonenrol'] = 0;
695 $course['category'] = $course['categoryid'];
697 //Note: create_course() core function check shortname, idnumber, category
698 $course['id'] = create_course((object) $course)->id
;
700 $resultcourses[] = array('id' => $course['id'], 'shortname' => $course['shortname']);
703 $transaction->allow_commit();
705 return $resultcourses;
709 * Returns description of method result value
711 * @return external_description
714 public static function create_courses_returns() {
715 return new external_multiple_structure(
716 new external_single_structure(
718 'id' => new external_value(PARAM_INT
, 'course id'),
719 'shortname' => new external_value(PARAM_TEXT
, 'short name'),
726 * Returns description of method parameters
727 * @return external_function_parameters
729 public static function delete_courses_parameters() {
730 return new external_function_parameters(
732 'courseids' => new external_multiple_structure(new external_value(PARAM_INT
, 'course ID')),
739 * @param array $courseids A list of course ids
741 public static function delete_courses($courseids) {
743 require_once($CFG->dirroot
."/course/lib.php");
745 // Parameter validation.
746 $params = self
::validate_parameters(self
::delete_courses_parameters(), array('courseids'=>$courseids));
748 $transaction = $DB->start_delegated_transaction();
750 foreach ($params['courseids'] as $courseid) {
751 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST
);
753 // Check if the context is valid.
754 $coursecontext = context_course
::instance($course->id
);
755 self
::validate_context($coursecontext);
757 // Check if the current user has enought permissions.
758 if (!can_delete_course($courseid)) {
759 throw new moodle_exception('cannotdeletecategorycourse', 'error', '', format_string($course->fullname
)." (id: $courseid)");
762 delete_course($course, false);
765 $transaction->allow_commit();
771 * Returns description of method result value
772 * @return external_description
774 public static function delete_courses_returns() {
781 * Deprecated course external functions
783 * @package core_course
784 * @copyright 2009 Petr Skodak
785 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
787 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
788 * @todo MDL-31194 This will be deleted in Moodle 2.5.
789 * @see core_course_external
791 class moodle_course_external
extends external_api
{
794 * Returns description of method parameters
796 * @return external_function_parameters
798 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
799 * @todo MDL-31194 This will be deleted in Moodle 2.5.
800 * @see core_course_external::get_courses_parameters()
802 public static function get_courses_parameters() {
803 return core_course_external
::get_courses_parameters();
809 * @param array $options
812 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
813 * @todo MDL-31194 This will be deleted in Moodle 2.5.
814 * @see core_course_external::get_courses()
816 public static function get_courses($options) {
817 return core_course_external
::get_courses($options);
821 * Returns description of method result value
823 * @return external_description
825 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
826 * @todo MDL-31194 This will be deleted in Moodle 2.5.
827 * @see core_course_external::get_courses_returns()
829 public static function get_courses_returns() {
830 return core_course_external
::get_courses_returns();
834 * Returns description of method parameters
836 * @return external_function_parameters
838 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
839 * @todo MDL-31194 This will be deleted in Moodle 2.5.
840 * @see core_course_external::create_courses_parameters()
842 public static function create_courses_parameters() {
843 return core_course_external
::create_courses_parameters();
849 * @param array $courses
850 * @return array courses (id and shortname only)
852 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
853 * @todo MDL-31194 This will be deleted in Moodle 2.5.
854 * @see core_course_external::create_courses()
856 public static function create_courses($courses) {
857 return core_course_external
::create_courses($courses);
861 * Returns description of method result value
863 * @return external_description
865 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
866 * @todo MDL-31194 This will be deleted in Moodle 2.5.
867 * @see core_course_external::create_courses_returns()
869 public static function create_courses_returns() {
870 return core_course_external
::create_courses_returns();