PageRenderTime 67ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/enrol/ajax.php

https://github.com/andreev-artem/moodle
PHP | 113 lines | 71 code | 13 blank | 29 comment | 13 complexity | 8d850c46bb93207a65b12891015f7387 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-3.0, Apache-2.0
  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. * This file processes AJAX enrolment actions and returns JSON
  18. *
  19. * The general idea behind this file is that any errors should throw exceptions
  20. * which will be returned and acted upon by the calling AJAX script.
  21. *
  22. * @package core
  23. * @subpackage enrol
  24. * @copyright 2010 Sam Hemelryk
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26. */
  27. define('AJAX_SCRIPT', true);
  28. require('../config.php');
  29. require_once("$CFG->dirroot/enrol/locallib.php");
  30. require_once("$CFG->dirroot/enrol/renderer.php");
  31. require_once("$CFG->dirroot/group/lib.php");
  32. // Must have the sesskey
  33. $id = required_param('id', PARAM_INT); // course id
  34. $action = required_param('action', PARAM_ACTION);
  35. $PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action)));
  36. $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
  37. $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
  38. if ($course->id == SITEID) {
  39. throw new moodle_exception('invalidcourse');
  40. }
  41. require_login($course);
  42. require_capability('moodle/course:enrolreview', $context);
  43. require_sesskey();
  44. echo $OUTPUT->header(); // send headers
  45. $manager = new course_enrolment_manager($PAGE, $course);
  46. $outcome = new stdClass();
  47. $outcome->success = true;
  48. $outcome->response = new stdClass();
  49. $outcome->error = '';
  50. switch ($action) {
  51. case 'unenrol':
  52. $ue = $DB->get_record('user_enrolments', array('id'=>required_param('ue', PARAM_INT)), '*', MUST_EXIST);
  53. list ($instance, $plugin) = $manager->get_user_enrolment_components($ue);
  54. if (!$instance || !$plugin || !$plugin->allow_unenrol_user($instance, $ue) || !has_capability("enrol/$instance->enrol:unenrol", $manager->get_context()) || !$manager->unenrol_user($ue)) {
  55. throw new enrol_ajax_exception('unenrolnotpermitted');
  56. }
  57. break;
  58. case 'unassign':
  59. $role = required_param('role', PARAM_INT);
  60. $user = required_param('user', PARAM_INT);
  61. if (!has_capability('moodle/role:assign', $manager->get_context()) || !$manager->unassign_role_from_user($user, $role)) {
  62. throw new enrol_ajax_exception('unassignnotpermitted');
  63. }
  64. break;
  65. case 'assign':
  66. $user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST);
  67. $roleid = required_param('roleid', PARAM_INT);
  68. if (!array_key_exists($roleid, $manager->get_assignable_roles())) {
  69. throw new enrol_ajax_exception('invalidrole');
  70. }
  71. if (!has_capability('moodle/role:assign', $manager->get_context()) || !$manager->assign_role_to_user($roleid, $user->id)) {
  72. throw new enrol_ajax_exception('assignnotpermitted');
  73. }
  74. $outcome->response->roleid = $roleid;
  75. break;
  76. case 'getassignable':
  77. $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
  78. $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
  79. break;
  80. case 'searchotherusers':
  81. $search = optional_param('search', '', PARAM_RAW);
  82. $page = optional_param('page', 0, PARAM_INT);
  83. $outcome->response = $manager->search_other_users($search, false, $page);
  84. foreach ($outcome->response['users'] as &$user) {
  85. $user->userId = $user->id;
  86. $user->picture = $OUTPUT->user_picture($user);
  87. $user->fullname = fullname($user);
  88. unset($user->id);
  89. }
  90. // Chrome will display users in the order of the array keys, so we need
  91. // to ensure that the results ordered array keys. Fortunately, the JavaScript
  92. // does not care what the array keys are. It uses user.id where necessary.
  93. $outcome->response['users'] = array_values($outcome->response['users']);
  94. $outcome->success = true;
  95. break;
  96. default:
  97. throw new enrol_ajax_exception('unknowajaxaction');
  98. }
  99. echo json_encode($outcome);
  100. die();