[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 18 /** 19 * External groups API 20 * 21 * @package core_group 22 * @category external 23 * @copyright 2009 Petr Skodak 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 27 require_once("$CFG->libdir/externallib.php"); 28 29 /** 30 * Group external functions 31 * 32 * @package core_group 33 * @category external 34 * @copyright 2011 Jerome Mouneyrac 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 * @since Moodle 2.2 37 */ 38 class core_group_external extends external_api { 39 40 /** 41 * Returns description of method parameters 42 * 43 * @return external_function_parameters 44 * @since Moodle 2.2 45 */ 46 public static function create_groups_parameters() { 47 return new external_function_parameters( 48 array( 49 'groups' => new external_multiple_structure( 50 new external_single_structure( 51 array( 52 'courseid' => new external_value(PARAM_INT, 'id of course'), 53 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), 54 'description' => new external_value(PARAM_RAW, 'group description text'), 55 'descriptionformat' => new external_format_value('description', VALUE_DEFAULT), 56 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase', VALUE_OPTIONAL), 57 ) 58 ), 'List of group object. A group has a courseid, a name, a description and an enrolment key.' 59 ) 60 ) 61 ); 62 } 63 64 /** 65 * Create groups 66 * 67 * @param array $groups array of group description arrays (with keys groupname and courseid) 68 * @return array of newly created groups 69 * @since Moodle 2.2 70 */ 71 public static function create_groups($groups) { 72 global $CFG, $DB; 73 require_once("$CFG->dirroot/group/lib.php"); 74 75 $params = self::validate_parameters(self::create_groups_parameters(), array('groups'=>$groups)); 76 77 $transaction = $DB->start_delegated_transaction(); 78 79 $groups = array(); 80 81 foreach ($params['groups'] as $group) { 82 $group = (object)$group; 83 84 if (trim($group->name) == '') { 85 throw new invalid_parameter_exception('Invalid group name'); 86 } 87 if ($DB->get_record('groups', array('courseid'=>$group->courseid, 'name'=>$group->name))) { 88 throw new invalid_parameter_exception('Group with the same name already exists in the course'); 89 } 90 91 // now security checks 92 $context = context_course::instance($group->courseid, IGNORE_MISSING); 93 try { 94 self::validate_context($context); 95 } catch (Exception $e) { 96 $exceptionparam = new stdClass(); 97 $exceptionparam->message = $e->getMessage(); 98 $exceptionparam->courseid = $group->courseid; 99 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 100 } 101 require_capability('moodle/course:managegroups', $context); 102 103 // Validate format. 104 $group->descriptionformat = external_validate_format($group->descriptionformat); 105 106 // finally create the group 107 $group->id = groups_create_group($group, false); 108 if (!isset($group->enrolmentkey)) { 109 $group->enrolmentkey = ''; 110 } 111 $groups[] = (array)$group; 112 } 113 114 $transaction->allow_commit(); 115 116 return $groups; 117 } 118 119 /** 120 * Returns description of method result value 121 * 122 * @return external_description 123 * @since Moodle 2.2 124 */ 125 public static function create_groups_returns() { 126 return new external_multiple_structure( 127 new external_single_structure( 128 array( 129 'id' => new external_value(PARAM_INT, 'group record id'), 130 'courseid' => new external_value(PARAM_INT, 'id of course'), 131 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), 132 'description' => new external_value(PARAM_RAW, 'group description text'), 133 'descriptionformat' => new external_format_value('description'), 134 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'), 135 ) 136 ), 'List of group object. A group has an id, a courseid, a name, a description and an enrolment key.' 137 ); 138 } 139 140 /** 141 * Returns description of method parameters 142 * 143 * @return external_function_parameters 144 * @since Moodle 2.2 145 */ 146 public static function get_groups_parameters() { 147 return new external_function_parameters( 148 array( 149 'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID') 150 ,'List of group id. A group id is an integer.'), 151 ) 152 ); 153 } 154 155 /** 156 * Get groups definition specified by ids 157 * 158 * @param array $groupids arrays of group ids 159 * @return array of group objects (id, courseid, name, enrolmentkey) 160 * @since Moodle 2.2 161 */ 162 public static function get_groups($groupids) { 163 $params = self::validate_parameters(self::get_groups_parameters(), array('groupids'=>$groupids)); 164 165 $groups = array(); 166 foreach ($params['groupids'] as $groupid) { 167 // validate params 168 $group = groups_get_group($groupid, 'id, courseid, name, description, descriptionformat, enrolmentkey', MUST_EXIST); 169 170 // now security checks 171 $context = context_course::instance($group->courseid, IGNORE_MISSING); 172 try { 173 self::validate_context($context); 174 } catch (Exception $e) { 175 $exceptionparam = new stdClass(); 176 $exceptionparam->message = $e->getMessage(); 177 $exceptionparam->courseid = $group->courseid; 178 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 179 } 180 require_capability('moodle/course:managegroups', $context); 181 182 list($group->description, $group->descriptionformat) = 183 external_format_text($group->description, $group->descriptionformat, 184 $context->id, 'group', 'description', $group->id); 185 186 $groups[] = (array)$group; 187 } 188 189 return $groups; 190 } 191 192 /** 193 * Returns description of method result value 194 * 195 * @return external_description 196 * @since Moodle 2.2 197 */ 198 public static function get_groups_returns() { 199 return new external_multiple_structure( 200 new external_single_structure( 201 array( 202 'id' => new external_value(PARAM_INT, 'group record id'), 203 'courseid' => new external_value(PARAM_INT, 'id of course'), 204 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), 205 'description' => new external_value(PARAM_RAW, 'group description text'), 206 'descriptionformat' => new external_format_value('description'), 207 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'), 208 ) 209 ) 210 ); 211 } 212 213 /** 214 * Returns description of method parameters 215 * 216 * @return external_function_parameters 217 * @since Moodle 2.2 218 */ 219 public static function get_course_groups_parameters() { 220 return new external_function_parameters( 221 array( 222 'courseid' => new external_value(PARAM_INT, 'id of course'), 223 ) 224 ); 225 } 226 227 /** 228 * Get all groups in the specified course 229 * 230 * @param int $courseid id of course 231 * @return array of group objects (id, courseid, name, enrolmentkey) 232 * @since Moodle 2.2 233 */ 234 public static function get_course_groups($courseid) { 235 $params = self::validate_parameters(self::get_course_groups_parameters(), array('courseid'=>$courseid)); 236 237 // now security checks 238 $context = context_course::instance($params['courseid'], IGNORE_MISSING); 239 try { 240 self::validate_context($context); 241 } catch (Exception $e) { 242 $exceptionparam = new stdClass(); 243 $exceptionparam->message = $e->getMessage(); 244 $exceptionparam->courseid = $params['courseid']; 245 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 246 } 247 require_capability('moodle/course:managegroups', $context); 248 249 $gs = groups_get_all_groups($params['courseid'], 0, 0, 250 'g.id, g.courseid, g.name, g.description, g.descriptionformat, g.enrolmentkey'); 251 252 $groups = array(); 253 foreach ($gs as $group) { 254 list($group->description, $group->descriptionformat) = 255 external_format_text($group->description, $group->descriptionformat, 256 $context->id, 'group', 'description', $group->id); 257 $groups[] = (array)$group; 258 } 259 260 return $groups; 261 } 262 263 /** 264 * Returns description of method result value 265 * 266 * @return external_description 267 * @since Moodle 2.2 268 */ 269 public static function get_course_groups_returns() { 270 return new external_multiple_structure( 271 new external_single_structure( 272 array( 273 'id' => new external_value(PARAM_INT, 'group record id'), 274 'courseid' => new external_value(PARAM_INT, 'id of course'), 275 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), 276 'description' => new external_value(PARAM_RAW, 'group description text'), 277 'descriptionformat' => new external_format_value('description'), 278 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'), 279 ) 280 ) 281 ); 282 } 283 284 /** 285 * Returns description of method parameters 286 * 287 * @return external_function_parameters 288 * @since Moodle 2.2 289 */ 290 public static function delete_groups_parameters() { 291 return new external_function_parameters( 292 array( 293 'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID')), 294 ) 295 ); 296 } 297 298 /** 299 * Delete groups 300 * 301 * @param array $groupids array of group ids 302 * @since Moodle 2.2 303 */ 304 public static function delete_groups($groupids) { 305 global $CFG, $DB; 306 require_once("$CFG->dirroot/group/lib.php"); 307 308 $params = self::validate_parameters(self::delete_groups_parameters(), array('groupids'=>$groupids)); 309 310 $transaction = $DB->start_delegated_transaction(); 311 312 foreach ($params['groupids'] as $groupid) { 313 // validate params 314 $groupid = validate_param($groupid, PARAM_INT); 315 if (!$group = groups_get_group($groupid, '*', IGNORE_MISSING)) { 316 // silently ignore attempts to delete nonexisting groups 317 continue; 318 } 319 320 // now security checks 321 $context = context_course::instance($group->courseid, IGNORE_MISSING); 322 try { 323 self::validate_context($context); 324 } catch (Exception $e) { 325 $exceptionparam = new stdClass(); 326 $exceptionparam->message = $e->getMessage(); 327 $exceptionparam->courseid = $group->courseid; 328 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 329 } 330 require_capability('moodle/course:managegroups', $context); 331 332 groups_delete_group($group); 333 } 334 335 $transaction->allow_commit(); 336 } 337 338 /** 339 * Returns description of method result value 340 * 341 * @return null 342 * @since Moodle 2.2 343 */ 344 public static function delete_groups_returns() { 345 return null; 346 } 347 348 349 /** 350 * Returns description of method parameters 351 * 352 * @return external_function_parameters 353 * @since Moodle 2.2 354 */ 355 public static function get_group_members_parameters() { 356 return new external_function_parameters( 357 array( 358 'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID')), 359 ) 360 ); 361 } 362 363 /** 364 * Return all members for a group 365 * 366 * @param array $groupids array of group ids 367 * @return array with group id keys containing arrays of user ids 368 * @since Moodle 2.2 369 */ 370 public static function get_group_members($groupids) { 371 $members = array(); 372 373 $params = self::validate_parameters(self::get_group_members_parameters(), array('groupids'=>$groupids)); 374 375 foreach ($params['groupids'] as $groupid) { 376 // validate params 377 $group = groups_get_group($groupid, 'id, courseid, name, enrolmentkey', MUST_EXIST); 378 // now security checks 379 $context = context_course::instance($group->courseid, IGNORE_MISSING); 380 try { 381 self::validate_context($context); 382 } catch (Exception $e) { 383 $exceptionparam = new stdClass(); 384 $exceptionparam->message = $e->getMessage(); 385 $exceptionparam->courseid = $group->courseid; 386 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 387 } 388 require_capability('moodle/course:managegroups', $context); 389 390 $groupmembers = groups_get_members($group->id, 'u.id', 'lastname ASC, firstname ASC'); 391 392 $members[] = array('groupid'=>$groupid, 'userids'=>array_keys($groupmembers)); 393 } 394 395 return $members; 396 } 397 398 /** 399 * Returns description of method result value 400 * 401 * @return external_description 402 * @since Moodle 2.2 403 */ 404 public static function get_group_members_returns() { 405 return new external_multiple_structure( 406 new external_single_structure( 407 array( 408 'groupid' => new external_value(PARAM_INT, 'group record id'), 409 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user id')), 410 ) 411 ) 412 ); 413 } 414 415 416 /** 417 * Returns description of method parameters 418 * 419 * @return external_function_parameters 420 * @since Moodle 2.2 421 */ 422 public static function add_group_members_parameters() { 423 return new external_function_parameters( 424 array( 425 'members'=> new external_multiple_structure( 426 new external_single_structure( 427 array( 428 'groupid' => new external_value(PARAM_INT, 'group record id'), 429 'userid' => new external_value(PARAM_INT, 'user id'), 430 ) 431 ) 432 ) 433 ) 434 ); 435 } 436 437 /** 438 * Add group members 439 * 440 * @param array $members of arrays with keys userid, groupid 441 * @since Moodle 2.2 442 */ 443 public static function add_group_members($members) { 444 global $CFG, $DB; 445 require_once("$CFG->dirroot/group/lib.php"); 446 447 $params = self::validate_parameters(self::add_group_members_parameters(), array('members'=>$members)); 448 449 $transaction = $DB->start_delegated_transaction(); 450 foreach ($params['members'] as $member) { 451 // validate params 452 $groupid = $member['groupid']; 453 $userid = $member['userid']; 454 455 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST); 456 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST); 457 458 // now security checks 459 $context = context_course::instance($group->courseid, IGNORE_MISSING); 460 try { 461 self::validate_context($context); 462 } catch (Exception $e) { 463 $exceptionparam = new stdClass(); 464 $exceptionparam->message = $e->getMessage(); 465 $exceptionparam->courseid = $group->courseid; 466 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 467 } 468 require_capability('moodle/course:managegroups', $context); 469 470 // now make sure user is enrolled in course - this is mandatory requirement, 471 // unfortunately this is slow 472 if (!is_enrolled($context, $userid)) { 473 throw new invalid_parameter_exception('Only enrolled users may be members of groups'); 474 } 475 476 groups_add_member($group, $user); 477 } 478 479 $transaction->allow_commit(); 480 } 481 482 /** 483 * Returns description of method result value 484 * 485 * @return null 486 * @since Moodle 2.2 487 */ 488 public static function add_group_members_returns() { 489 return null; 490 } 491 492 493 /** 494 * Returns description of method parameters 495 * 496 * @return external_function_parameters 497 * @since Moodle 2.2 498 */ 499 public static function delete_group_members_parameters() { 500 return new external_function_parameters( 501 array( 502 'members'=> new external_multiple_structure( 503 new external_single_structure( 504 array( 505 'groupid' => new external_value(PARAM_INT, 'group record id'), 506 'userid' => new external_value(PARAM_INT, 'user id'), 507 ) 508 ) 509 ) 510 ) 511 ); 512 } 513 514 /** 515 * Delete group members 516 * 517 * @param array $members of arrays with keys userid, groupid 518 * @since Moodle 2.2 519 */ 520 public static function delete_group_members($members) { 521 global $CFG, $DB; 522 require_once("$CFG->dirroot/group/lib.php"); 523 524 $params = self::validate_parameters(self::delete_group_members_parameters(), array('members'=>$members)); 525 526 $transaction = $DB->start_delegated_transaction(); 527 528 foreach ($params['members'] as $member) { 529 // validate params 530 $groupid = $member['groupid']; 531 $userid = $member['userid']; 532 533 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST); 534 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST); 535 536 // now security checks 537 $context = context_course::instance($group->courseid, IGNORE_MISSING); 538 try { 539 self::validate_context($context); 540 } catch (Exception $e) { 541 $exceptionparam = new stdClass(); 542 $exceptionparam->message = $e->getMessage(); 543 $exceptionparam->courseid = $group->courseid; 544 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 545 } 546 require_capability('moodle/course:managegroups', $context); 547 548 if (!groups_remove_member_allowed($group, $user)) { 549 throw new moodle_exception('errorremovenotpermitted', 'group', '', fullname($user)); 550 } 551 groups_remove_member($group, $user); 552 } 553 554 $transaction->allow_commit(); 555 } 556 557 /** 558 * Returns description of method result value 559 * 560 * @return null 561 * @since Moodle 2.2 562 */ 563 public static function delete_group_members_returns() { 564 return null; 565 } 566 567 /** 568 * Returns description of method parameters 569 * 570 * @return external_function_parameters 571 * @since Moodle 2.3 572 */ 573 public static function create_groupings_parameters() { 574 return new external_function_parameters( 575 array( 576 'groupings' => new external_multiple_structure( 577 new external_single_structure( 578 array( 579 'courseid' => new external_value(PARAM_INT, 'id of course'), 580 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), 581 'description' => new external_value(PARAM_RAW, 'grouping description text'), 582 'descriptionformat' => new external_format_value('description', VALUE_DEFAULT) 583 ) 584 ), 'List of grouping object. A grouping has a courseid, a name and a description.' 585 ) 586 ) 587 ); 588 } 589 590 /** 591 * Create groupings 592 * 593 * @param array $groupings array of grouping description arrays (with keys groupname and courseid) 594 * @return array of newly created groupings 595 * @since Moodle 2.3 596 */ 597 public static function create_groupings($groupings) { 598 global $CFG, $DB; 599 require_once("$CFG->dirroot/group/lib.php"); 600 601 $params = self::validate_parameters(self::create_groupings_parameters(), array('groupings'=>$groupings)); 602 603 $transaction = $DB->start_delegated_transaction(); 604 605 $groupings = array(); 606 607 foreach ($params['groupings'] as $grouping) { 608 $grouping = (object)$grouping; 609 610 if (trim($grouping->name) == '') { 611 throw new invalid_parameter_exception('Invalid grouping name'); 612 } 613 if ($DB->count_records('groupings', array('courseid'=>$grouping->courseid, 'name'=>$grouping->name))) { 614 throw new invalid_parameter_exception('Grouping with the same name already exists in the course'); 615 } 616 617 // Now security checks . 618 $context = context_course::instance($grouping->courseid); 619 try { 620 self::validate_context($context); 621 } catch (Exception $e) { 622 $exceptionparam = new stdClass(); 623 $exceptionparam->message = $e->getMessage(); 624 $exceptionparam->courseid = $grouping->courseid; 625 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 626 } 627 require_capability('moodle/course:managegroups', $context); 628 629 $grouping->descriptionformat = external_validate_format($grouping->descriptionformat); 630 631 // Finally create the grouping. 632 $grouping->id = groups_create_grouping($grouping); 633 $groupings[] = (array)$grouping; 634 } 635 636 $transaction->allow_commit(); 637 638 return $groupings; 639 } 640 641 /** 642 * Returns description of method result value 643 * 644 * @return external_description 645 * @since Moodle 2.3 646 */ 647 public static function create_groupings_returns() { 648 return new external_multiple_structure( 649 new external_single_structure( 650 array( 651 'id' => new external_value(PARAM_INT, 'grouping record id'), 652 'courseid' => new external_value(PARAM_INT, 'id of course'), 653 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), 654 'description' => new external_value(PARAM_RAW, 'grouping description text'), 655 'descriptionformat' => new external_format_value('description') 656 ) 657 ), 'List of grouping object. A grouping has an id, a courseid, a name and a description.' 658 ); 659 } 660 661 /** 662 * Returns description of method parameters 663 * 664 * @return external_function_parameters 665 * @since Moodle 2.3 666 */ 667 public static function update_groupings_parameters() { 668 return new external_function_parameters( 669 array( 670 'groupings' => new external_multiple_structure( 671 new external_single_structure( 672 array( 673 'id' => new external_value(PARAM_INT, 'id of grouping'), 674 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), 675 'description' => new external_value(PARAM_RAW, 'grouping description text'), 676 'descriptionformat' => new external_format_value('description', VALUE_DEFAULT) 677 ) 678 ), 'List of grouping object. A grouping has a courseid, a name and a description.' 679 ) 680 ) 681 ); 682 } 683 684 /** 685 * Update groupings 686 * 687 * @param array $groupings array of grouping description arrays (with keys groupname and courseid) 688 * @return array of newly updated groupings 689 * @since Moodle 2.3 690 */ 691 public static function update_groupings($groupings) { 692 global $CFG, $DB; 693 require_once("$CFG->dirroot/group/lib.php"); 694 695 $params = self::validate_parameters(self::update_groupings_parameters(), array('groupings'=>$groupings)); 696 697 $transaction = $DB->start_delegated_transaction(); 698 699 foreach ($params['groupings'] as $grouping) { 700 $grouping = (object)$grouping; 701 702 if (trim($grouping->name) == '') { 703 throw new invalid_parameter_exception('Invalid grouping name'); 704 } 705 706 if (! $currentgrouping = $DB->get_record('groupings', array('id'=>$grouping->id))) { 707 throw new invalid_parameter_exception("Grouping $grouping->id does not exist in the course"); 708 } 709 710 // Check if the new modified grouping name already exists in the course. 711 if ($grouping->name != $currentgrouping->name and 712 $DB->count_records('groupings', array('courseid'=>$currentgrouping->courseid, 'name'=>$grouping->name))) { 713 throw new invalid_parameter_exception('A different grouping with the same name already exists in the course'); 714 } 715 716 $grouping->courseid = $currentgrouping->courseid; 717 718 // Now security checks. 719 $context = context_course::instance($grouping->courseid); 720 try { 721 self::validate_context($context); 722 } catch (Exception $e) { 723 $exceptionparam = new stdClass(); 724 $exceptionparam->message = $e->getMessage(); 725 $exceptionparam->courseid = $grouping->courseid; 726 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 727 } 728 require_capability('moodle/course:managegroups', $context); 729 730 // We must force allways FORMAT_HTML. 731 $grouping->descriptionformat = external_validate_format($grouping->descriptionformat); 732 733 // Finally update the grouping. 734 groups_update_grouping($grouping); 735 } 736 737 $transaction->allow_commit(); 738 739 return null; 740 } 741 742 /** 743 * Returns description of method result value 744 * 745 * @return external_description 746 * @since Moodle 2.3 747 */ 748 public static function update_groupings_returns() { 749 return null; 750 } 751 752 /** 753 * Returns description of method parameters 754 * 755 * @return external_function_parameters 756 * @since Moodle 2.3 757 */ 758 public static function get_groupings_parameters() { 759 return new external_function_parameters( 760 array( 761 'groupingids' => new external_multiple_structure(new external_value(PARAM_INT, 'grouping ID') 762 , 'List of grouping id. A grouping id is an integer.'), 763 'returngroups' => new external_value(PARAM_BOOL, 'return associated groups', VALUE_DEFAULT, 0) 764 ) 765 ); 766 } 767 768 /** 769 * Get groupings definition specified by ids 770 * 771 * @param array $groupingids arrays of grouping ids 772 * @param boolean $returngroups return the associated groups if true. The default is false. 773 * @return array of grouping objects (id, courseid, name) 774 * @since Moodle 2.3 775 */ 776 public static function get_groupings($groupingids, $returngroups = false) { 777 global $CFG, $DB; 778 require_once("$CFG->dirroot/group/lib.php"); 779 require_once("$CFG->libdir/filelib.php"); 780 781 $params = self::validate_parameters(self::get_groupings_parameters(), 782 array('groupingids' => $groupingids, 783 'returngroups' => $returngroups)); 784 785 $groupings = array(); 786 foreach ($params['groupingids'] as $groupingid) { 787 // Validate params. 788 $grouping = groups_get_grouping($groupingid, '*', MUST_EXIST); 789 790 // Now security checks. 791 $context = context_course::instance($grouping->courseid); 792 try { 793 self::validate_context($context); 794 } catch (Exception $e) { 795 $exceptionparam = new stdClass(); 796 $exceptionparam->message = $e->getMessage(); 797 $exceptionparam->courseid = $grouping->courseid; 798 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 799 } 800 require_capability('moodle/course:managegroups', $context); 801 802 list($grouping->description, $grouping->descriptionformat) = 803 external_format_text($grouping->description, $grouping->descriptionformat, 804 $context->id, 'grouping', 'description', $grouping->id); 805 806 $groupingarray = (array)$grouping; 807 808 if ($params['returngroups']) { 809 $grouprecords = $DB->get_records_sql("SELECT * FROM {groups} g INNER JOIN {groupings_groups} gg ". 810 "ON g.id = gg.groupid WHERE gg.groupingid = ? ". 811 "ORDER BY groupid", array($groupingid)); 812 if ($grouprecords) { 813 $groups = array(); 814 foreach ($grouprecords as $grouprecord) { 815 list($grouprecord->description, $grouprecord->descriptionformat) = 816 external_format_text($grouprecord->description, $grouprecord->descriptionformat, 817 $context->id, 'group', 'description', $grouprecord->groupid); 818 $groups[] = array('id' => $grouprecord->groupid, 819 'name' => $grouprecord->name, 820 'description' => $grouprecord->description, 821 'descriptionformat' => $grouprecord->descriptionformat, 822 'enrolmentkey' => $grouprecord->enrolmentkey, 823 'courseid' => $grouprecord->courseid 824 ); 825 } 826 $groupingarray['groups'] = $groups; 827 } 828 } 829 $groupings[] = $groupingarray; 830 } 831 832 return $groupings; 833 } 834 835 /** 836 * Returns description of method result value 837 * 838 * @return external_description 839 * @since Moodle 2.3 840 */ 841 public static function get_groupings_returns() { 842 return new external_multiple_structure( 843 new external_single_structure( 844 array( 845 'id' => new external_value(PARAM_INT, 'grouping record id'), 846 'courseid' => new external_value(PARAM_INT, 'id of course'), 847 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), 848 'description' => new external_value(PARAM_RAW, 'grouping description text'), 849 'descriptionformat' => new external_format_value('description'), 850 'groups' => new external_multiple_structure( 851 new external_single_structure( 852 array( 853 'id' => new external_value(PARAM_INT, 'group record id'), 854 'courseid' => new external_value(PARAM_INT, 'id of course'), 855 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), 856 'description' => new external_value(PARAM_RAW, 'group description text'), 857 'descriptionformat' => new external_format_value('description'), 858 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase') 859 ) 860 ), 861 'optional groups', VALUE_OPTIONAL) 862 ) 863 ) 864 ); 865 } 866 867 /** 868 * Returns description of method parameters 869 * 870 * @return external_function_parameters 871 * @since Moodle 2.3 872 */ 873 public static function get_course_groupings_parameters() { 874 return new external_function_parameters( 875 array( 876 'courseid' => new external_value(PARAM_INT, 'id of course'), 877 ) 878 ); 879 } 880 881 /** 882 * Get all groupings in the specified course 883 * 884 * @param int $courseid id of course 885 * @return array of grouping objects (id, courseid, name, enrolmentkey) 886 * @since Moodle 2.3 887 */ 888 public static function get_course_groupings($courseid) { 889 global $CFG; 890 require_once("$CFG->dirroot/group/lib.php"); 891 require_once("$CFG->libdir/filelib.php"); 892 893 $params = self::validate_parameters(self::get_course_groupings_parameters(), array('courseid'=>$courseid)); 894 895 // Now security checks. 896 $context = context_course::instance($params['courseid']); 897 898 try { 899 self::validate_context($context); 900 } catch (Exception $e) { 901 $exceptionparam = new stdClass(); 902 $exceptionparam->message = $e->getMessage(); 903 $exceptionparam->courseid = $params['courseid']; 904 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 905 } 906 require_capability('moodle/course:managegroups', $context); 907 908 $gs = groups_get_all_groupings($params['courseid']); 909 910 $groupings = array(); 911 foreach ($gs as $grouping) { 912 list($grouping->description, $grouping->descriptionformat) = 913 external_format_text($grouping->description, $grouping->descriptionformat, 914 $context->id, 'grouping', 'description', $grouping->id); 915 $groupings[] = (array)$grouping; 916 } 917 918 return $groupings; 919 } 920 921 /** 922 * Returns description of method result value 923 * 924 * @return external_description 925 * @since Moodle 2.3 926 */ 927 public static function get_course_groupings_returns() { 928 return new external_multiple_structure( 929 new external_single_structure( 930 array( 931 'id' => new external_value(PARAM_INT, 'grouping record id'), 932 'courseid' => new external_value(PARAM_INT, 'id of course'), 933 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), 934 'description' => new external_value(PARAM_RAW, 'grouping description text'), 935 'descriptionformat' => new external_format_value('description') 936 ) 937 ) 938 ); 939 } 940 941 /** 942 * Returns description of method parameters 943 * 944 * @return external_function_parameters 945 * @since Moodle 2.3 946 */ 947 public static function delete_groupings_parameters() { 948 return new external_function_parameters( 949 array( 950 'groupingids' => new external_multiple_structure(new external_value(PARAM_INT, 'grouping ID')), 951 ) 952 ); 953 } 954 955 /** 956 * Delete groupings 957 * 958 * @param array $groupingids array of grouping ids 959 * @return void 960 * @since Moodle 2.3 961 */ 962 public static function delete_groupings($groupingids) { 963 global $CFG, $DB; 964 require_once("$CFG->dirroot/group/lib.php"); 965 966 $params = self::validate_parameters(self::delete_groupings_parameters(), array('groupingids'=>$groupingids)); 967 968 $transaction = $DB->start_delegated_transaction(); 969 970 foreach ($params['groupingids'] as $groupingid) { 971 972 if (!$grouping = groups_get_grouping($groupingid, 'id, courseid', IGNORE_MISSING)) { 973 // Silently ignore attempts to delete nonexisting groupings. 974 continue; 975 } 976 977 // Now security checks. 978 $context = context_course::instance($grouping->courseid); 979 try { 980 self::validate_context($context); 981 } catch (Exception $e) { 982 $exceptionparam = new stdClass(); 983 $exceptionparam->message = $e->getMessage(); 984 $exceptionparam->courseid = $grouping->courseid; 985 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 986 } 987 require_capability('moodle/course:managegroups', $context); 988 989 groups_delete_grouping($grouping); 990 } 991 992 $transaction->allow_commit(); 993 } 994 995 /** 996 * Returns description of method result value 997 * 998 * @return external_description 999 * @since Moodle 2.3 1000 */ 1001 public static function delete_groupings_returns() { 1002 return null; 1003 } 1004 1005 /** 1006 * Returns description of method parameters 1007 * 1008 * @return external_function_parameters 1009 * @since Moodle 2.3 1010 */ 1011 public static function assign_grouping_parameters() { 1012 return new external_function_parameters( 1013 array( 1014 'assignments'=> new external_multiple_structure( 1015 new external_single_structure( 1016 array( 1017 'groupingid' => new external_value(PARAM_INT, 'grouping record id'), 1018 'groupid' => new external_value(PARAM_INT, 'group record id'), 1019 ) 1020 ) 1021 ) 1022 ) 1023 ); 1024 } 1025 1026 /** 1027 * Assign a group to a grouping 1028 * 1029 * @param array $assignments of arrays with keys groupid, groupingid 1030 * @return void 1031 * @since Moodle 2.3 1032 */ 1033 public static function assign_grouping($assignments) { 1034 global $CFG, $DB; 1035 require_once("$CFG->dirroot/group/lib.php"); 1036 1037 $params = self::validate_parameters(self::assign_grouping_parameters(), array('assignments'=>$assignments)); 1038 1039 $transaction = $DB->start_delegated_transaction(); 1040 foreach ($params['assignments'] as $assignment) { 1041 // Validate params. 1042 $groupingid = $assignment['groupingid']; 1043 $groupid = $assignment['groupid']; 1044 1045 $grouping = groups_get_grouping($groupingid, 'id, courseid', MUST_EXIST); 1046 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST); 1047 1048 if ($DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) { 1049 // Continue silently if the group is yet assigned to the grouping. 1050 continue; 1051 } 1052 1053 // Now security checks. 1054 $context = context_course::instance($grouping->courseid); 1055 try { 1056 self::validate_context($context); 1057 } catch (Exception $e) { 1058 $exceptionparam = new stdClass(); 1059 $exceptionparam->message = $e->getMessage(); 1060 $exceptionparam->courseid = $group->courseid; 1061 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 1062 } 1063 require_capability('moodle/course:managegroups', $context); 1064 1065 groups_assign_grouping($groupingid, $groupid); 1066 } 1067 1068 $transaction->allow_commit(); 1069 } 1070 1071 /** 1072 * Returns description of method result value 1073 * 1074 * @return null 1075 * @since Moodle 2.3 1076 */ 1077 public static function assign_grouping_returns() { 1078 return null; 1079 } 1080 1081 /** 1082 * Returns description of method parameters 1083 * 1084 * @return external_function_parameters 1085 * @since Moodle 2.3 1086 */ 1087 public static function unassign_grouping_parameters() { 1088 return new external_function_parameters( 1089 array( 1090 'unassignments'=> new external_multiple_structure( 1091 new external_single_structure( 1092 array( 1093 'groupingid' => new external_value(PARAM_INT, 'grouping record id'), 1094 'groupid' => new external_value(PARAM_INT, 'group record id'), 1095 ) 1096 ) 1097 ) 1098 ) 1099 ); 1100 } 1101 1102 /** 1103 * Unassign a group from a grouping 1104 * 1105 * @param array $unassignments of arrays with keys groupid, groupingid 1106 * @return void 1107 * @since Moodle 2.3 1108 */ 1109 public static function unassign_grouping($unassignments) { 1110 global $CFG, $DB; 1111 require_once("$CFG->dirroot/group/lib.php"); 1112 1113 $params = self::validate_parameters(self::unassign_grouping_parameters(), array('unassignments'=>$unassignments)); 1114 1115 $transaction = $DB->start_delegated_transaction(); 1116 foreach ($params['unassignments'] as $unassignment) { 1117 // Validate params. 1118 $groupingid = $unassignment['groupingid']; 1119 $groupid = $unassignment['groupid']; 1120 1121 $grouping = groups_get_grouping($groupingid, 'id, courseid', MUST_EXIST); 1122 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST); 1123 1124 if (!$DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) { 1125 // Continue silently if the group is not assigned to the grouping. 1126 continue; 1127 } 1128 1129 // Now security checks. 1130 $context = context_course::instance($grouping->courseid); 1131 try { 1132 self::validate_context($context); 1133 } catch (Exception $e) { 1134 $exceptionparam = new stdClass(); 1135 $exceptionparam->message = $e->getMessage(); 1136 $exceptionparam->courseid = $group->courseid; 1137 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 1138 } 1139 require_capability('moodle/course:managegroups', $context); 1140 1141 groups_unassign_grouping($groupingid, $groupid); 1142 } 1143 1144 $transaction->allow_commit(); 1145 } 1146 1147 /** 1148 * Returns description of method result value 1149 * 1150 * @return null 1151 * @since Moodle 2.3 1152 */ 1153 public static function unassign_grouping_returns() { 1154 return null; 1155 } 1156 1157 } 1158 1159 /** 1160 * Deprecated group external functions 1161 * 1162 * @package core_group 1163 * @copyright 2009 Petr Skodak 1164 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 1165 * @since Moodle 2.0 1166 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more. 1167 * @see core_group_external 1168 */ 1169 class moodle_group_external extends external_api { 1170 1171 /** 1172 * Returns description of method parameters 1173 * 1174 * @return external_function_parameters 1175 * @since Moodle 2.0 1176 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1177 * @see core_group_external::create_groups_parameters() 1178 */ 1179 public static function create_groups_parameters() { 1180 return core_group_external::create_groups_parameters(); 1181 } 1182 1183 /** 1184 * Create groups 1185 * 1186 * @param array $groups array of group description arrays (with keys groupname and courseid) 1187 * @return array of newly created groups 1188 * @since Moodle 2.0 1189 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1190 * @see use core_group_external::create_groups() 1191 */ 1192 public static function create_groups($groups) { 1193 return core_group_external::create_groups($groups); 1194 } 1195 1196 /** 1197 * Returns description of method result value 1198 * 1199 * @return external_description 1200 * @since Moodle 2.0 1201 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1202 * @see core_group_external::create_groups_returns() 1203 */ 1204 public static function create_groups_returns() { 1205 return core_group_external::create_groups_returns(); 1206 } 1207 1208 /** 1209 * Returns description of method parameters 1210 * 1211 * @return external_function_parameters 1212 * @since Moodle 2.0 1213 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1214 * @see core_group_external::get_groups_parameters() 1215 */ 1216 public static function get_groups_parameters() { 1217 return core_group_external::get_groups_parameters(); 1218 } 1219 1220 /** 1221 * Get groups definition specified by ids 1222 * 1223 * @param array $groupids arrays of group ids 1224 * @return array of group objects (id, courseid, name, enrolmentkey) 1225 * @since Moodle 2.0 1226 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1227 * @see core_group_external::get_groups() 1228 */ 1229 public static function get_groups($groupids) { 1230 return core_group_external::get_groups($groupids); 1231 } 1232 1233 /** 1234 * Returns description of method result value 1235 * 1236 * @return external_description 1237 * @since Moodle 2.0 1238 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1239 * @see core_group_external::get_groups_returns() 1240 */ 1241 public static function get_groups_returns() { 1242 return core_group_external::get_groups_returns(); 1243 } 1244 1245 /** 1246 * Returns description of method parameters 1247 * 1248 * @return external_function_parameters 1249 * @since Moodle 2.0 1250 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1251 * @see core_group_external::get_course_groups_parameters() 1252 */ 1253 public static function get_course_groups_parameters() { 1254 return core_group_external::get_course_groups_parameters(); 1255 } 1256 1257 /** 1258 * Get all groups in the specified course 1259 * 1260 * @param int $courseid id of course 1261 * @return array of group objects (id, courseid, name, enrolmentkey) 1262 * @since Moodle 2.0 1263 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1264 * @see core_group_external::get_course_groups() 1265 */ 1266 public static function get_course_groups($courseid) { 1267 return core_group_external::get_course_groups($courseid); 1268 } 1269 1270 /** 1271 * Returns description of method result value 1272 * 1273 * @return external_description 1274 * @since Moodle 2.0 1275 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1276 * @see core_group_external::get_course_groups_returns() 1277 */ 1278 public static function get_course_groups_returns() { 1279 return core_group_external::get_course_groups_returns(); 1280 } 1281 1282 /** 1283 * Returns description of method parameters 1284 * 1285 * @return external_function_parameters 1286 * @since Moodle 2.0 1287 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1288 * @see core_group_external::delete_group_members_parameters() 1289 */ 1290 public static function delete_groups_parameters() { 1291 return core_group_external::delete_group_members_parameters(); 1292 } 1293 1294 /** 1295 * Delete groups 1296 * 1297 * @param array $groupids array of group ids 1298 * @since Moodle 2.0 1299 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1300 * @see core_group_external::delete_groups() 1301 */ 1302 public static function delete_groups($groupids) { 1303 return core_group_external::delete_groups($groupids); 1304 } 1305 1306 /** 1307 * Returns description of method result value 1308 * 1309 * @return null 1310 * @since Moodle 2.0 1311 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1312 * @see core_group_external::delete_group_members_returns() 1313 */ 1314 public static function delete_groups_returns() { 1315 return core_group_external::delete_group_members_returns(); 1316 } 1317 1318 1319 /** 1320 * Returns description of method parameters 1321 * 1322 * @return external_function_parameters 1323 * @since Moodle 2.0 1324 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1325 * @see core_group_external::get_group_members_parameters() 1326 */ 1327 public static function get_groupmembers_parameters() { 1328 return core_group_external::get_group_members_parameters(); 1329 } 1330 1331 /** 1332 * Return all members for a group 1333 * 1334 * @param array $groupids array of group ids 1335 * @return array with group id keys containing arrays of user ids 1336 * @since Moodle 2.0 1337 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1338 * @see core_group_external::get_group_members() 1339 */ 1340 public static function get_groupmembers($groupids) { 1341 return core_group_external::get_group_members($groupids); 1342 } 1343 1344 /** 1345 * Returns description of method result value 1346 * 1347 * @return external_description 1348 * @since Moodle 2.0 1349 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1350 * @see core_group_external::get_group_members_returns() 1351 */ 1352 public static function get_groupmembers_returns() { 1353 return core_group_external::get_group_members_returns(); 1354 } 1355 1356 1357 /** 1358 * Returns description of method parameters 1359 * 1360 * @return external_function_parameters 1361 * @since Moodle 2.0 1362 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1363 * @see core_group_external::add_group_members_parameters() 1364 */ 1365 public static function add_groupmembers_parameters() { 1366 return core_group_external::add_group_members_parameters(); 1367 } 1368 1369 /** 1370 * Add group members 1371 * 1372 * @param array $members of arrays with keys userid, groupid 1373 * @since Moodle 2.0 1374 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1375 * @see use core_group_external::add_group_members() 1376 */ 1377 public static function add_groupmembers($members) { 1378 return core_group_external::add_group_members($members); 1379 } 1380 1381 /** 1382 * Returns description of method result value 1383 * 1384 * @return external_description 1385 * @since Moodle 2.0 1386 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1387 * @see core_group_external::add_group_members_returns() 1388 */ 1389 public static function add_groupmembers_returns() { 1390 return core_group_external::add_group_members_returns(); 1391 } 1392 1393 1394 /** 1395 * Returns description of method parameters 1396 * 1397 * @return external_function_parameters 1398 * @since Moodle 2.0 1399 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1400 * @see core_group_external::delete_group_members_parameters() 1401 */ 1402 public static function delete_groupmembers_parameters() { 1403 return core_group_external::delete_group_members_parameters(); 1404 } 1405 1406 /** 1407 * Delete group members 1408 * 1409 * @param array $members of arrays with keys userid, groupid 1410 * @since Moodle 2.0 1411 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1412 * @see core_group_external::delete_group_members() 1413 */ 1414 public static function delete_groupmembers($members) { 1415 return core_group_external::delete_group_members($members); 1416 } 1417 1418 /** 1419 * Returns description of method result value 1420 * 1421 * @return external_description 1422 * @since Moodle 2.0 1423 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1424 * @see core_group_external::delete_group_members_returns() 1425 */ 1426 public static function delete_groupmembers_returns() { 1427 return core_group_external::delete_group_members_returns(); 1428 } 1429 1430 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |