Permalink
Browse files

Merge branch 'wip-MDL-23532-master' of git://github.com/samhemelryk/m…

…oodle
  • Loading branch information...
skodak committed Apr 25, 2011
2 parents 6e405da + 884faff commit 6bc62c4486d67fc0bcbfcff9db80c65a1797dfc6
Showing with 1,574 additions and 627 deletions.
  1. +1 −97 enrol/ajax.php
  2. +108 −0 enrol/cohort/ajax.php
  3. +57 −10 enrol/cohort/lib.php
  4. +87 −0 enrol/cohort/locallib.php
  5. 0 ...ins/sam/quickcohortenrolment.css → cohort/yui/quickenrolment/assets/skins/sam/quickenrolment.css}
  6. BIN enrol/{yui/quickcohortenrolment → cohort/yui/quickenrolment}/assets/skins/sam/sprite.png
  7. +4 −4 .../{yui/quickcohortenrolment/quickcohortenrolment.js → cohort/yui/quickenrolment/quickenrolment.js}
  8. +262 −139 enrol/locallib.php
  9. +132 −0 enrol/manual/ajax.php
  10. +110 −0 enrol/manual/editenrolment.php
  11. +81 −0 enrol/manual/editenrolment_form.php
  12. +3 −0 enrol/manual/lang/en/enrol_manual.php
  13. +101 −0 enrol/manual/lib.php
  14. +5 −5 enrol/manual/locallib.php
  15. +96 −0 enrol/manual/unenroluser.php
  16. +1 −0 ...s/skins/sam/enrolmentmanager.css → manual/yui/quickenrolment/assets/skins/sam/quickenrolment.css}
  17. BIN enrol/{yui/enrolmentmanager → manual/yui/quickenrolment}/assets/skins/sam/sprite.png
  18. +60 −59 enrol/{yui/enrolmentmanager/enrolmentmanager.js → manual/yui/quickenrolment/quickenrolment.js}
  19. +1 −1 enrol/otherusers.php
  20. +107 −200 enrol/renderer.php
  21. +113 −0 enrol/self/editenrolment.php
  22. +81 −0 enrol/self/editenrolment_form.php
  23. +3 −0 enrol/self/lang/en/enrol_self.php
  24. +24 −0 enrol/self/lib.php
  25. +101 −0 enrol/self/unenroluser.php
  26. +4 −44 enrol/users.php
  27. +1 −67 enrol/users_forms.php
  28. +1 −0 lang/en/enrol.php
  29. +25 −1 lib/enrollib.php
  30. +5 −0 theme/base/style/core.css
View
@@ -34,7 +34,6 @@
require_once("$CFG->dirroot/group/lib.php");
// Must have the sesskey
-require_sesskey();
$id = required_param('id', PARAM_INT); // course id
$action = required_param('action', PARAM_ACTION);
@@ -53,7 +52,7 @@
echo $OUTPUT->header(); // send headers
-$manager = new course_enrolment_manager($course);
+$manager = new course_enrolment_manager($PAGE, $course);
$outcome = new stdClass;
$outcome->success = true;
@@ -75,7 +74,6 @@
throw new enrol_ajax_exception('unassignnotpermitted');
}
break;
-
case 'assign':
$user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST);
$roleid = required_param('roleid', PARAM_INT);
@@ -87,53 +85,10 @@
}
$outcome->response->roleid = $roleid;
break;
-
case 'getassignable':
$otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
$outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
break;
- case 'getdefaultcohortrole': //TODO: use in ajax UI MDL-24280
- $cohortenrol = enrol_get_plugin('cohort');
- $outcome->response = $cohortenrol->get_config('roleid');
- break;
- case 'getcohorts':
- require_capability('moodle/course:enrolconfig', $context);
- $outcome->response = $manager->get_cohorts();
- break;
- case 'enrolcohort':
- require_capability('moodle/course:enrolconfig', $context);
- $roleid = required_param('roleid', PARAM_INT);
- $cohortid = required_param('cohortid', PARAM_INT);
- if (!$manager->enrol_cohort($cohortid, $roleid)) {
- throw new enrol_ajax_exception('errorenrolcohort');
- }
- break;
- case 'enrolcohortusers':
- require_capability('moodle/course:enrolconfig', $context);
- $roleid = required_param('roleid', PARAM_INT);
- $cohortid = required_param('cohortid', PARAM_INT);
- $result = $manager->enrol_cohort_users($cohortid, $roleid);
- if ($result === false) {
- throw new enrol_ajax_exception('errorenrolcohortusers');
- }
- $outcome->success = true;
- $outcome->response->users = $result;
- $outcome->response->title = get_string('success');
- $outcome->response->message = get_string('enrollednewusers', 'enrol', $result);
- $outcome->response->yesLabel = get_string('ok');
- break;
- case 'searchusers':
- $enrolid = required_param('enrolid', PARAM_INT);
- $search = optional_param('search', '', PARAM_RAW);
- $page = optional_param('page', 0, PARAM_INT);
- $outcome->response = $manager->get_potential_users($enrolid, $search, true, $page);
- foreach ($outcome->response['users'] as &$user) {
- $user->picture = $OUTPUT->user_picture($user);
- $user->fullname = fullname($user);
- }
- $outcome->success = true;
- break;
-
case 'searchotherusers':
$search = optional_param('search', '', PARAM_RAW);
$page = optional_param('page', 0, PARAM_INT);
@@ -146,57 +101,6 @@
}
$outcome->success = true;
break;
-
- case 'enrol':
- $enrolid = required_param('enrolid', PARAM_INT);
- $userid = required_param('userid', PARAM_INT);
-
- $roleid = optional_param('role', null, PARAM_INT);
- $duration = optional_param('duration', 0, PARAM_INT);
- $startdate = optional_param('startdate', 0, PARAM_INT);
- $recovergrades = optional_param('recovergrades', 0, PARAM_INT);
-
- if (empty($roleid)) {
- $roleid = null;
- }
-
- switch($startdate) {
- case 2:
- $timestart = $course->startdate;
- break;
- case 3:
- default:
- $today = time();
- $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
- $timestart = $today;
- break;
- }
- if ($duration <= 0) {
- $timeend = 0;
- } else {
- $timeend = $timestart + ($duration*24*60*60);
- }
-
- $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
- $instances = $manager->get_enrolment_instances();
- $plugins = $manager->get_enrolment_plugins();
- if (!array_key_exists($enrolid, $instances)) {
- throw new enrol_ajax_exception('invalidenrolinstance');
- }
- $instance = $instances[$enrolid];
- $plugin = $plugins[$instance->enrol];
- if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
- $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
- if ($recovergrades) {
- require_once($CFG->libdir.'/gradelib.php');
- grade_recover_history_grades($user->id, $instance->courseid);
- }
- } else {
- throw new enrol_ajax_exception('enrolnotpermitted');
- }
- $outcome->success = true;
- break;
-
default:
throw new enrol_ajax_exception('unknowajaxaction');
}
View
@@ -0,0 +1,108 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * This file processes AJAX enrolment actions and returns JSON for the cohort plugin
+ *
+ * The general idea behind this file is that any errors should throw exceptions
+ * which will be returned and acted upon by the calling AJAX script.
+ *
+ * @package enrol
+ * @subpackage cohort
+ * @copyright 2011 Sam Hemelryk
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+define('AJAX_SCRIPT', true);
+
+require('../../config.php');
+require_once($CFG->dirroot.'/enrol/locallib.php');
+require_once($CFG->dirroot.'/enrol/cohort/locallib.php');
+require_once($CFG->dirroot.'/group/lib.php');
+
+// Must have the sesskey
+$id = required_param('id', PARAM_INT); // course id
+$action = required_param('action', PARAM_ACTION);
+
+$PAGE->set_url(new moodle_url('/enrol/cohort/ajax.php', array('id'=>$id, 'action'=>$action)));
+
+$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
+$context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
+
+if ($course->id == SITEID) {
+ throw new moodle_exception('invalidcourse');
+}
+
+require_login($course);
+require_capability('moodle/course:enrolreview', $context);
+require_sesskey();
+
+echo $OUTPUT->header(); // send headers
+
+$manager = new course_enrolment_manager($PAGE, $course);
+
+$outcome = new stdClass;
+$outcome->success = true;
+$outcome->response = new stdClass;
+$outcome->error = '';
+
+switch ($action) {
+ case 'getassignable':
+ $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
+ $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
+ break;
+ case 'getdefaultcohortrole': //TODO: use in ajax UI MDL-24280
+ $cohortenrol = enrol_get_plugin('cohort');
+ $outcome->response = $cohortenrol->get_config('roleid');
+ break;
+ case 'getcohorts':
+ require_capability('moodle/course:enrolconfig', $context);
+ $outcome->response = enrol_cohort_get_cohorts($manager);
+ break;
+ case 'enrolcohort':
+ require_capability('moodle/course:enrolconfig', $context);
+ $roleid = required_param('roleid', PARAM_INT);
+ $cohortid = required_param('cohortid', PARAM_INT);
+
+ $roles = $manager->get_assignable_roles();
+ $cohorts = enrol_cohort_get_cohorts($manager);
+ if (!array_key_exists($cohortid, $cohorts) || !array_key_exists($roleid, $roles)) {
+ throw new enrol_ajax_exception('errorenrolcohort');
+ }
+ $enrol = enrol_get_plugin('cohort');
+ $enrol->add_instance($manager->get_course(), array('customint1' => $cohortid, 'roleid' => $roleid));
+ enrol_cohort_sync($manager->get_course()->id);
+ break;
+ case 'enrolcohortusers':
+ require_capability('moodle/course:enrolconfig', $context);
+ $roleid = required_param('roleid', PARAM_INT);
+ $cohortid = required_param('cohortid', PARAM_INT);
+ $result = enrol_cohort_enrol_all_users($manager, $cohortid, $roleid);
+ if ($result === false) {
+ throw new enrol_ajax_exception('errorenrolcohortusers');
+ }
+ $outcome->success = true;
+ $outcome->response->users = $result;
+ $outcome->response->title = get_string('success');
+ $outcome->response->message = get_string('enrollednewusers', 'enrol', $result);
+ $outcome->response->yesLabel = get_string('ok');
+ break;
+ default:
+ throw new enrol_ajax_exception('unknowajaxaction');
+}
+
+echo json_encode($outcome);
+die();
View
@@ -64,32 +64,40 @@ public function get_instance_name($instance) {
* @return moodle_url page url
*/
public function get_newinstance_link($courseid) {
+ if (!$this->can_add_new_instances($courseid)) {
+ return NULL;
+ }
+ // multiple instances supported - multiple parent courses linked
+ return new moodle_url('/enrol/cohort/addinstance.php', array('id'=>$courseid));
+ }
+
+ /**
+ * Given a courseid this function returns true if the user is able to enrol or configure cohorts
+ * AND there are cohorts that the user can view.
+ *
+ * @param int $courseid
+ * @return bool
+ */
+ protected function can_add_new_instances($courseid) {
global $DB;
$coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/cohort:config', $coursecontext)) {
- return NULL;
+ return false;
}
-
list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($coursecontext));
$sql = "SELECT id, contextid
FROM {cohort}
WHERE contextid $sqlparents
ORDER BY name ASC";
$cohorts = $DB->get_records_sql($sql, $params);
- $found = false;
foreach ($cohorts as $c) {
$context = get_context_instance_by_id($c->contextid);
if (has_capability('moodle/cohort:view', $context)) {
- $found = true;
- break;
+ return true;
}
}
- if (!$found) {
- return NULL;
- }
- // multiple instances supported - multiple parent courses linked
- return new moodle_url('/enrol/cohort/addinstance.php', array('id'=>$courseid));
+ return false;
}
/**
@@ -129,6 +137,45 @@ public function course_updated($inserted, $course, $data) {
}
}
+
+ /**
+ * Returns a button to enrol a cohort or its users through the manual enrolment plugin.
+ *
+ * This function also adds a quickenrolment JS ui to the page so that users can be enrolled
+ * via AJAX.
+ *
+ * @param course_enrolment_manager $manager
+ * @return enrol_user_button
+ */
+ public function get_manual_enrol_button(course_enrolment_manager $manager) {
+ $course = $manager->get_course();
+ if (!$this->can_add_new_instances($course->id)) {
+ return false;
+ }
+
+ $cohorturl = new moodle_url('/enrol/cohort/addinstance.php', array('id' => $course->id));
+ $button = new enrol_user_button($cohorturl, get_string('enrolcohort', 'enrol'), 'get');
+ $button->class .= ' enrol_cohort_plugin';
+
+ $button->strings_for_js(array('enrol','synced','enrolcohort','enrolcohortusers'), 'enrol');
+ $button->strings_for_js('assignroles', 'role');
+ $button->strings_for_js('cohort', 'cohort');
+ $button->strings_for_js('users', 'moodle');
+
+ // No point showing this at all if the user cant manually enrol users
+ $hasmanualinstance = has_capability('enrol/manual:manage', $manager->get_context()) && $manager->has_instance('manual');
+
+ $modules = array('moodle-enrol_cohort-quickenrolment', 'moodle-enrol_cohort-quickenrolment-skin');
+ $function = 'M.enrol_cohort.quickenrolment.init';
+ $arguments = array(
+ 'courseid' => $course->id,
+ 'ajaxurl' => '/enrol/cohort/ajax.php',
+ 'url' => $manager->get_moodlepage()->url->out(false),
+ 'manualEnrolment' => $hasmanualinstance);
+ $button->require_yui_module($modules, $function, array($arguments));
+
+ return $button;
+ }
}
Oops, something went wrong.

0 comments on commit 6bc62c4

Please sign in to comment.