function definition() { global $CFG, $DB; $mform = $this->_form; list($instance, $plugin, $course) = $this->_customdata; $coursecontext = context_course::instance($course->id); $enrol = enrol_get_plugin('cohort'); $groups = array(0 => get_string('none')); foreach (groups_get_all_groups($course->id) as $group) { $groups[$group->id] = format_string($group->name, true, array('context' => $coursecontext)); } $mform->addElement('header', 'general', get_string('pluginname', 'enrol_cohort')); $mform->addElement('text', 'name', get_string('custominstancename', 'enrol')); $mform->setType('name', PARAM_TEXT); $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'), ENROL_INSTANCE_DISABLED => get_string('no')); $mform->addElement('select', 'status', get_string('status', 'enrol_cohort'), $options); if ($instance->id) { if ($cohort = $DB->get_record('cohort', array('id' => $instance->customint1))) { $cohorts = array($instance->customint1 => format_string($cohort->name, true, array('context' => context::instance_by_id($cohort->contextid)))); } else { $cohorts = array($instance->customint1 => get_string('error')); } $mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $cohorts); $mform->setConstant('customint1', $instance->customint1); $mform->hardFreeze('customint1', $instance->customint1); } else { $cohorts = array('' => get_string('choosedots')); $allcohorts = cohort_get_available_cohorts($coursecontext, 0, 0, 0); foreach ($allcohorts as $c) { $cohorts[$c->id] = format_string($c->name); } $mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $cohorts); $mform->addRule('customint1', get_string('required'), 'required', null, 'client'); } $roles = get_assignable_roles($coursecontext); $roles[0] = get_string('none'); $roles = array_reverse($roles, true); // Descending default sortorder. $mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_cohort'), $roles); $mform->setDefault('roleid', $enrol->get_config('roleid')); if ($instance->id and !isset($roles[$instance->roleid])) { if ($role = $DB->get_record('role', array('id' => $instance->roleid))) { $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS, true); $roles[$instance->roleid] = role_get_name($role, $coursecontext); } else { $roles[$instance->roleid] = get_string('error'); } } $mform->addElement('select', 'customint2', get_string('addgroup', 'enrol_cohort'), $groups); $mform->addElement('hidden', 'courseid', null); $mform->setType('courseid', PARAM_INT); $mform->addElement('hidden', 'id', null); $mform->setType('id', PARAM_INT); if ($instance->id) { $this->add_action_buttons(true); } else { $this->add_action_buttons(true, get_string('addinstance', 'enrol')); } $this->set_data($instance); }
/** * Add elements to the edit instance form. * * @param stdClass $instance * @param MoodleQuickForm $mform * @param context $context * @return bool */ public function edit_instance_form($instance, MoodleQuickForm $mform, $context) { global $CFG; // Merge these two settings to one value for the single selection element. if ($instance->notifyall and $instance->expirynotify) { $instance->expirynotify = 2; } unset($instance->notifyall); $nameattribs = array('size' => '20', 'maxlength' => '255'); $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'), $nameattribs); $mform->setType('name', PARAM_TEXT); $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'server'); $options = $this->get_status_options(); $mform->addElement('select', 'status', get_string('status', 'enrol_self'), $options); $mform->addHelpButton('status', 'status', 'enrol_self'); $options = $this->get_newenrols_options(); $mform->addElement('select', 'customint6', get_string('newenrols', 'enrol_self'), $options); $mform->addHelpButton('customint6', 'newenrols', 'enrol_self'); $mform->disabledIf('customint6', 'status', 'eq', ENROL_INSTANCE_DISABLED); $passattribs = array('size' => '20', 'maxlength' => '50'); $mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_self'), $passattribs); $mform->addHelpButton('password', 'password', 'enrol_self'); if (empty($instance->id) and $this->get_config('requirepassword')) { $mform->addRule('password', get_string('required'), 'required', null, 'client'); } $mform->addRule('password', get_string('maximumchars', '', 50), 'maxlength', 50, 'server'); $options = $this->get_groupkey_options(); $mform->addElement('select', 'customint1', get_string('groupkey', 'enrol_self'), $options); $mform->addHelpButton('customint1', 'groupkey', 'enrol_self'); $roles = $this->extend_assignable_roles($context, $instance->roleid); $mform->addElement('select', 'roleid', get_string('role', 'enrol_self'), $roles); $options = array('optional' => true, 'defaultunit' => 86400); $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_self'), $options); $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_self'); $options = $this->get_expirynotify_options(); $mform->addElement('select', 'expirynotify', get_string('expirynotify', 'core_enrol'), $options); $mform->addHelpButton('expirynotify', 'expirynotify', 'core_enrol'); $options = array('optional' => false, 'defaultunit' => 86400); $mform->addElement('duration', 'expirythreshold', get_string('expirythreshold', 'core_enrol'), $options); $mform->addHelpButton('expirythreshold', 'expirythreshold', 'core_enrol'); $mform->disabledIf('expirythreshold', 'expirynotify', 'eq', 0); $options = array('optional' => true); $mform->addElement('date_time_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_self'), $options); $mform->setDefault('enrolstartdate', 0); $mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_self'); $options = array('optional' => true); $mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_self'), $options); $mform->setDefault('enrolenddate', 0); $mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_self'); $options = $this->get_longtimenosee_options(); $mform->addElement('select', 'customint2', get_string('longtimenosee', 'enrol_self'), $options); $mform->addHelpButton('customint2', 'longtimenosee', 'enrol_self'); $mform->addElement('text', 'customint3', get_string('maxenrolled', 'enrol_self')); $mform->addHelpButton('customint3', 'maxenrolled', 'enrol_self'); $mform->setType('customint3', PARAM_INT); require_once $CFG->dirroot . '/cohort/lib.php'; $cohorts = array(0 => get_string('no')); $allcohorts = cohort_get_available_cohorts($context, 0, 0, 0); if ($instance->customint5 && !isset($allcohorts[$instance->customint5])) { $c = $DB->get_record('cohort', array('id' => $instance->customint5), 'id, name, idnumber, contextid, visible', IGNORE_MISSING); if ($c) { // Current cohort was not found because current user can not see it. Still keep it. $allcohorts[$instance->customint5] = $c; } } foreach ($allcohorts as $c) { $cohorts[$c->id] = format_string($c->name, true, array('context' => context::instance_by_id($c->contextid))); if ($c->idnumber) { $cohorts[$c->id] .= ' [' . s($c->idnumber) . ']'; } } if ($instance->customint5 && !isset($allcohorts[$instance->customint5])) { // Somebody deleted a cohort, better keep the wrong value so that random ppl can not enrol. $cohorts[$instance->customint5] = get_string('unknowncohort', 'cohort', $instance->customint5); } if (count($cohorts) > 1) { $mform->addElement('select', 'customint5', get_string('cohortonly', 'enrol_self'), $cohorts); $mform->addHelpButton('customint5', 'cohortonly', 'enrol_self'); } else { $mform->addElement('hidden', 'customint5'); $mform->setType('customint5', PARAM_INT); $mform->setConstant('customint5', 0); } $mform->addElement('advcheckbox', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self')); $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self'); $options = array('cols' => '60', 'rows' => '8'); $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'enrol_self'), $options); $mform->addHelpButton('customtext1', 'customwelcomemessage', 'enrol_self'); if (enrol_accessing_via_instance($instance)) { $warntext = get_string('instanceeditselfwarningtext', 'core_enrol'); $mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), $warntext); } }
/** * Returns a button to manually enrol users through the manual enrolment plugin. * * By default the first manual enrolment plugin instance available in the course is used. * If no manual enrolment instances exist within the course then false is returned. * * 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) { global $CFG; require_once $CFG->dirroot . '/cohort/lib.php'; $instance = null; $instances = array(); foreach ($manager->get_enrolment_instances() as $tempinstance) { if ($tempinstance->enrol == 'manual') { if ($instance === null) { $instance = $tempinstance; } $instances[] = array('id' => $tempinstance->id, 'name' => $this->get_instance_name($tempinstance)); } } if (empty($instance)) { return false; } if (!($manuallink = $this->get_manual_enrol_link($instance))) { return false; } $button = new enrol_user_button($manuallink, get_string('enrolusers', 'enrol_manual'), 'get'); $button->class .= ' enrol_manual_plugin'; $startdate = $manager->get_course()->startdate; if (!($defaultstart = get_config('enrol_manual', 'enrolstart'))) { // Default to now if there is no system setting. $defaultstart = 4; } $startdateoptions = array(); $dateformat = get_string('strftimedatefullshort'); if ($startdate > 0) { $startdateoptions[2] = get_string('coursestart') . ' (' . userdate($startdate, $dateformat) . ')'; } $now = time(); $today = make_timestamp(date('Y', $now), date('m', $now), date('d', $now), 0, 0, 0); $startdateoptions[3] = get_string('today') . ' (' . userdate($today, $dateformat) . ')'; $startdateoptions[4] = get_string('now', 'enrol_manual') . ' (' . userdate($now, get_string('strftimedatetimeshort')) . ')'; $defaultduration = $instance->enrolperiod > 0 ? $instance->enrolperiod / 86400 : ''; $modules = array('moodle-enrol_manual-quickenrolment', 'moodle-enrol_manual-quickenrolment-skin'); $arguments = array('instances' => $instances, 'courseid' => $instance->courseid, 'ajaxurl' => '/enrol/manual/ajax.php', 'url' => $manager->get_moodlepage()->url->out(false), 'optionsStartDate' => $startdateoptions, 'defaultRole' => $instance->roleid, 'defaultDuration' => $defaultduration, 'defaultStartDate' => (int) $defaultstart, 'disableGradeHistory' => $CFG->disablegradehistory, 'recoverGradesDefault' => '', 'cohortsAvailable' => cohort_get_available_cohorts($manager->get_context(), COHORT_WITH_NOTENROLLED_MEMBERS_ONLY, 0, 1) ? true : false); if ($CFG->recovergradesdefault) { $arguments['recoverGradesDefault'] = ' checked="checked"'; } $function = 'M.enrol_manual.quickenrolment.init'; $button->require_yui_module($modules, $function, array($arguments)); $button->strings_for_js(array('ajaxoneuserfound', 'ajaxxusersfound', 'ajaxnext25', 'enrol', 'enrolmentoptions', 'enrolusers', 'enrolxusers', 'errajaxfailedenrol', 'errajaxsearch', 'foundxcohorts', 'none', 'usersearch', 'unlimitedduration', 'startdatetoday', 'durationdays', 'enrolperiod', 'finishenrollingusers', 'recovergrades'), 'enrol'); $button->strings_for_js(array('browseusers', 'browsecohorts'), 'enrol_manual'); $button->strings_for_js('assignroles', 'role'); $button->strings_for_js('startingfrom', 'moodle'); return $button; }
/** * Form Definition */ function definition() { global $CFG, $COURSE; $mform =& $this->_form; $mform->addElement('header', 'autogroup', get_string('general')); $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group')); $mform->addHelpButton('namingscheme', 'namingscheme', 'group'); $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client'); $mform->setType('namingscheme', PARAM_TEXT); // There must not be duplicate group names in course. $template = get_string('grouptemplate', 'group'); $gname = groups_parse_name($template, 0); if (!groups_get_group_by_name($COURSE->id, $gname)) { $mform->setDefault('namingscheme', $template); } $options = array('groups' => get_string('numgroups', 'group'), 'members' => get_string('nummembers', 'group')); $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options); $mform->addElement('text', 'number', get_string('number', 'group'), 'maxlength="4" size="4"'); $mform->setType('number', PARAM_INT); $mform->addRule('number', null, 'numeric', null, 'client'); $mform->addRule('number', get_string('required'), 'required', null, 'client'); $mform->addElement('header', 'groupmembershdr', get_string('groupmembers', 'group')); $mform->setExpanded('groupmembershdr', true); $options = array(0 => get_string('all')); $options += $this->_customdata['roles']; $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options); $student = get_archetype_roles('student'); $student = reset($student); if ($student and array_key_exists($student->id, $options)) { $mform->setDefault('roleid', $student->id); } if ($cohorts = cohort_get_available_cohorts(context_course::instance($COURSE->id), COHORT_WITH_ENROLLED_MEMBERS_ONLY)) { $options = array(0 => get_string('anycohort', 'cohort')); foreach ($cohorts as $c) { $options[$c->id] = format_string($c->name, true, context::instance_by_id($c->contextid)); } $mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options); $mform->setDefault('cohortid', '0'); } else { $mform->addElement('hidden', 'cohortid'); $mform->setType('cohortid', PARAM_INT); $mform->setConstant('cohortid', '0'); } if ($groupings = groups_get_all_groupings($COURSE->id)) { $options = array(); $options[0] = get_string('none'); foreach ($groupings as $grouping) { $options[$grouping->id] = format_string($grouping->name); } $mform->addElement('select', 'groupingid', get_string('selectfromgrouping', 'group'), $options); $mform->setDefault('groupingid', 0); $mform->disabledIf('groupingid', 'notingroup', 'checked'); } else { $mform->addElement('hidden', 'groupingid'); $mform->setType('groupingid', PARAM_INT); $mform->setConstant('groupingid', 0); } if ($groups = groups_get_all_groups($COURSE->id)) { $options = array(); $options[0] = get_string('none'); foreach ($groups as $group) { $options[$group->id] = format_string($group->name); } $mform->addElement('select', 'groupid', get_string('selectfromgroup', 'group'), $options); $mform->setDefault('groupid', 0); $mform->disabledIf('groupid', 'notingroup', 'checked'); } else { $mform->addElement('hidden', 'groupid'); $mform->setType('groupid', PARAM_INT); $mform->setConstant('groupid', 0); } $options = array('no' => get_string('noallocation', 'group'), 'random' => get_string('random', 'group'), 'firstname' => get_string('byfirstname', 'group'), 'lastname' => get_string('bylastname', 'group'), 'idnumber' => get_string('byidnumber', 'group')); $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options); $mform->setDefault('allocateby', 'random'); $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group')); $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members'); $mform->addElement('checkbox', 'notingroup', get_string('notingroup', 'group')); $mform->disabledIf('notingroup', 'groupingid', 'neq', 0); $mform->disabledIf('notingroup', 'groupid', 'neq', 0); $mform->addElement('header', 'groupinghdr', get_string('grouping', 'group')); $options = array('0' => get_string('nogrouping', 'group'), '-1' => get_string('newgrouping', 'group')); if ($groupings = groups_get_all_groupings($COURSE->id)) { foreach ($groupings as $grouping) { $options[$grouping->id] = strip_tags(format_string($grouping->name)); } } $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options); if ($groupings) { $mform->setDefault('grouping', '-1'); } $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options); $mform->setType('groupingname', PARAM_TEXT); $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1'); $mform->addElement('hidden', 'courseid'); $mform->setType('courseid', PARAM_INT); $mform->addElement('hidden', 'seed'); $mform->setType('seed', PARAM_INT); $buttonarray = array(); $buttonarray[] =& $mform->createElement('submit', 'preview', get_string('preview')); $buttonarray[] =& $mform->createElement('submit', 'submitbutton', get_string('submit')); $buttonarray[] =& $mform->createElement('cancel'); $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); $mform->closeHeaderBefore('buttonar'); }
public function test_cohort_get_available_cohorts() { global $DB; $this->resetAfterTest(); $category1 = $this->getDataGenerator()->create_category(); $category2 = $this->getDataGenerator()->create_category(); $course1 = $this->getDataGenerator()->create_course(array('category' => $category1->id)); $course2 = $this->getDataGenerator()->create_course(array('category' => $category2->id)); $category1ctx = context_coursecat::instance($category1->id); $category2ctx = context_coursecat::instance($category2->id); $course1ctx = context_course::instance($course1->id); $course2ctx = context_course::instance($course2->id); $systemctx = context_system::instance(); $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => $category1ctx->id, 'name' => 'aaagrrryyy', 'idnumber' => '', 'description' => '')); $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid' => $category1ctx->id, 'name' => 'bbb', 'idnumber' => '', 'description' => 'yyybrrr', 'visible' => 0)); $cohort3 = $this->getDataGenerator()->create_cohort(array('contextid' => $category2ctx->id, 'name' => 'ccc', 'idnumber' => 'xxarrrghyyy', 'description' => 'po_us')); $cohort4 = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id, 'name' => 'ddd')); $cohort5 = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id, 'visible' => 0, 'name' => 'eee')); /* Structure of generated course categories, courses and cohort: system -cohort4 (visible, has 3 members) -cohort5 (not visible, no members) category1 -cohort1 (visible, no members) -cohort2 (not visible, has 1 member) course1 category2 -cohort3 (visible, has 2 member) course2 In this test we call cohort_get_available_cohorts() for users with different roles and with different paramteres ($withmembers, $search, $offset, $limit) to make sure we go through all possible options of SQL query. */ // Admin can see visible and invisible cohorts defined in above contexts. $this->setAdminUser(); $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 0, ''); $this->assertEquals(array($cohort1->id, $cohort2->id, $cohort4->id, $cohort5->id), array_keys($result)); $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 2, ''); $this->assertEquals(array($cohort1->id, $cohort2->id), array_keys($result)); $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 1, 2, ''); $this->assertEquals(array($cohort2->id, $cohort4->id), array_keys($result)); $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 100, 'yyy'); $this->assertEquals(array($cohort1->id, $cohort2->id), array_keys($result)); $result = cohort_get_available_cohorts($course2ctx, COHORT_ALL, 0, 0, ''); $this->assertEquals(array($cohort3->id, $cohort4->id, $cohort5->id), array_keys($result)); $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY); $this->assertEmpty($result); $result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_MEMBERS_ONLY); $this->assertEmpty($result); // Get list of available cohorts as a teacher in the course. $user1 = $this->getDataGenerator()->create_user(); $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); role_assign($teacherrole->id, $user1->id, $course1ctx->id); role_assign($teacherrole->id, $user1->id, $course2ctx->id); $this->setUser($user1); $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 0, ''); $this->assertEquals(array($cohort1->id, $cohort4->id), array_keys($result)); $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 1, ''); $this->assertEquals(array($cohort1->id), array_keys($result)); $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 1, 1, ''); $this->assertEquals(array($cohort4->id), array_keys($result)); $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 100, 'yyy'); $this->assertEquals(array($cohort1->id), array_keys($result)); $result = cohort_get_available_cohorts($course2ctx, COHORT_ALL, 0, 0, ''); $this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result)); $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY); $this->assertEmpty($result); // Now add members to cohorts. $user2 = $this->getDataGenerator()->create_user(); $user3 = $this->getDataGenerator()->create_user(); $user4 = $this->getDataGenerator()->create_user(); $user5 = $this->getDataGenerator()->create_user(); $user6 = $this->getDataGenerator()->create_user(); cohort_add_member($cohort2->id, $user3->id); cohort_add_member($cohort3->id, $user2->id); cohort_add_member($cohort3->id, $user3->id); cohort_add_member($cohort4->id, $user4->id); cohort_add_member($cohort4->id, $user5->id); cohort_add_member($cohort4->id, $user6->id); // Check filtering non-empty cohorts as admin. $this->setAdminUser(); $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, ''); $this->assertEquals(array($cohort2->id, $cohort4->id), array_keys($result)); $this->assertEquals(1, $result[$cohort2->id]->memberscnt); $this->assertEquals(3, $result[$cohort4->id]->memberscnt); $result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, ''); $this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result)); $this->assertEquals(2, $result[$cohort3->id]->memberscnt); $this->assertEquals(3, $result[$cohort4->id]->memberscnt); $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, 'yyy'); $this->assertEquals(array($cohort2->id), array_keys($result)); $this->assertEquals(1, $result[$cohort2->id]->memberscnt); // Check filtering non-empty cohorts as teacher. $this->setUser($user1); $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, ''); $this->assertEquals(array($cohort4->id), array_keys($result)); $this->assertEquals(3, $result[$cohort4->id]->memberscnt); $result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, ''); $this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result)); $this->assertEquals(2, $result[$cohort3->id]->memberscnt); $this->assertEquals(3, $result[$cohort4->id]->memberscnt); $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, 'yyy'); $this->assertEmpty($result); // Enrol users. $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user2->id, $course1->id, $studentrole->id); $this->getDataGenerator()->enrol_user($user3->id, $course1->id, $studentrole->id); $this->getDataGenerator()->enrol_user($user5->id, $course1->id, $studentrole->id); $this->getDataGenerator()->enrol_user($user6->id, $course1->id, $studentrole->id); $this->getDataGenerator()->enrol_user($user3->id, $course2->id, $studentrole->id); $this->getDataGenerator()->enrol_user($user4->id, $course2->id, $studentrole->id); $this->getDataGenerator()->enrol_user($user5->id, $course2->id, $studentrole->id); $this->getDataGenerator()->enrol_user($user6->id, $course2->id, $studentrole->id); // Check cohorts with enrolments as admin. $this->setAdminUser(); $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0, ''); $this->assertEquals(array($cohort2->id, $cohort4->id), array_keys($result)); $this->assertEquals(1, $result[$cohort2->id]->enrolledcnt); $this->assertEquals(2, $result[$cohort4->id]->enrolledcnt); $this->assertEquals(1, $result[$cohort2->id]->memberscnt); $this->assertEquals(3, $result[$cohort4->id]->memberscnt); $result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0, ''); $this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result)); $this->assertEquals(1, $result[$cohort3->id]->enrolledcnt); $this->assertEquals(3, $result[$cohort4->id]->enrolledcnt); $this->assertEquals(2, $result[$cohort3->id]->memberscnt); $this->assertEquals(3, $result[$cohort4->id]->memberscnt); $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0, 'yyy'); $this->assertEquals(array($cohort2->id), array_keys($result)); $this->assertEquals(1, $result[$cohort2->id]->enrolledcnt); $this->assertEquals(1, $result[$cohort2->id]->memberscnt); $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_NOTENROLLED_MEMBERS_ONLY, 0, 0, ''); $this->assertEquals(array($cohort4->id), array_keys($result)); $this->assertEquals(2, $result[$cohort4->id]->enrolledcnt); $this->assertEquals(3, $result[$cohort4->id]->memberscnt); // Assign user1 additional 'manager' role in the category context. He can now see hidden cohort in category1 // but still can not see hidden category in system. $managerrole = $DB->get_record('role', array('shortname' => 'manager')); role_assign($managerrole->id, $user1->id, context_coursecat::instance($category1->id)); $this->setUser($user1); $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 0, ''); $this->assertEquals(array($cohort1->id, $cohort2->id, $cohort4->id), array_keys($result)); }
/** * Gets cohorts the user is able to view. * * @deprecated since Moodle 2.8 MDL-35618 this functionality is removed * * @global moodle_database $DB * @param course_enrolment_manager $manager * @param int $offset limit output from * @param int $limit items to output per load * @param string $search search string * @return array Array(more => bool, offset => int, cohorts => array) */ function enrol_cohort_search_cohorts(course_enrolment_manager $manager, $offset = 0, $limit = 25, $search = '') { global $CFG; debugging('enrol_cohort_search_cohorts() is deprecated. This functionality is moved to enrol_manual.', DEBUG_DEVELOPER); require_once $CFG->dirroot . '/cohort/lib.php'; $context = $manager->get_context(); $cohorts = array(); $instances = $manager->get_enrolment_instances(); $enrolled = array(); foreach ($instances as $instance) { if ($instance->enrol === 'cohort') { $enrolled[] = $instance->customint1; } } $rawcohorts = cohort_get_available_cohorts($context, COHORT_COUNT_MEMBERS, $offset, $limit, $search); // Produce the output respecting parameters. foreach ($rawcohorts as $c) { $cohorts[$c->id] = array('cohortid' => $c->id, 'name' => shorten_text(format_string($c->name, true, array('context' => context::instance_by_id($c->contextid))), 35), 'users' => $c->memberscnt, 'enrolled' => in_array($c->id, $enrolled)); } return array('more' => !(bool) $limit, 'offset' => $offset, 'cohorts' => $cohorts); }
/** * Gets an array of the cohorts that can be enrolled in this course. * * @param int $enrolid * @param string $search * @param int $page Defaults to 0 * @param int $perpage Defaults to 25 * @param int $addedenrollment * @return array Array(totalcohorts => int, cohorts => array) */ function enrol_manual_get_potential_cohorts($context, $enrolid, $search = '', $page = 0, $perpage = 25, $addedenrollment = 0) { global $CFG; require_once $CFG->dirroot . '/cohort/lib.php'; $allcohorts = cohort_get_available_cohorts($context, COHORT_WITH_NOTENROLLED_MEMBERS_ONLY, 0, 0, $search); $totalcohorts = count($allcohorts); $cohorts = array(); $cnt = 0; foreach ($allcohorts as $c) { if ($cnt >= $page * $perpage && (!$perpage || $cnt < ($page + 1) * $perpage)) { $cohorts[] = (object) array('id' => $c->id, 'name' => format_string($c->name, true, array('context' => $c->contextid)), 'cnt' => $c->memberscnt - $c->enrolledcnt); } $cnt++; } return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts); }
function definition() { global $DB; $mform = $this->_form; list($instance, $plugin, $context) = $this->_customdata; $mform->addElement('header', 'header', get_string('pluginname', 'enrol_self')); $nameattribs = array('size' => '20', 'maxlength' => '255'); $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'), $nameattribs); $mform->setType('name', PARAM_TEXT); $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'server'); $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'), ENROL_INSTANCE_DISABLED => get_string('no')); $mform->addElement('select', 'status', get_string('status', 'enrol_self'), $options); $mform->addHelpButton('status', 'status', 'enrol_self'); $options = array(1 => get_string('yes'), 0 => get_string('no')); $mform->addElement('select', 'customint6', get_string('newenrols', 'enrol_self'), $options); $mform->addHelpButton('customint6', 'newenrols', 'enrol_self'); $mform->disabledIf('customint6', 'status', 'eq', ENROL_INSTANCE_DISABLED); $passattribs = array('size' => '20', 'maxlength' => '50'); $mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_self'), $passattribs); $mform->addHelpButton('password', 'password', 'enrol_self'); if (empty($instance->id) and $plugin->get_config('requirepassword')) { $mform->addRule('password', get_string('required'), 'required', null, 'client'); } $mform->addRule('password', get_string('maximumchars', '', 50), 'maxlength', 50, 'server'); $options = array(1 => get_string('yes'), 0 => get_string('no')); $mform->addElement('select', 'customint1', get_string('groupkey', 'enrol_self'), $options); $mform->addHelpButton('customint1', 'groupkey', 'enrol_self'); $roles = $this->extend_assignable_roles($context, $instance->roleid); $mform->addElement('select', 'roleid', get_string('role', 'enrol_self'), $roles); $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_self'), array('optional' => true, 'defaultunit' => 86400)); $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_self'); $options = array(0 => get_string('no'), 1 => get_string('expirynotifyenroller', 'core_enrol'), 2 => get_string('expirynotifyall', 'core_enrol')); $mform->addElement('select', 'expirynotify', get_string('expirynotify', 'core_enrol'), $options); $mform->addHelpButton('expirynotify', 'expirynotify', 'core_enrol'); $mform->addElement('duration', 'expirythreshold', get_string('expirythreshold', 'core_enrol'), array('optional' => false, 'defaultunit' => 86400)); $mform->addHelpButton('expirythreshold', 'expirythreshold', 'core_enrol'); $mform->disabledIf('expirythreshold', 'expirynotify', 'eq', 0); $mform->addElement('date_time_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_self'), array('optional' => true)); $mform->setDefault('enrolstartdate', 0); $mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_self'); $mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_self'), array('optional' => true)); $mform->setDefault('enrolenddate', 0); $mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_self'); $options = array(0 => get_string('never'), 1800 * 3600 * 24 => get_string('numdays', '', 1800), 1000 * 3600 * 24 => get_string('numdays', '', 1000), 365 * 3600 * 24 => get_string('numdays', '', 365), 180 * 3600 * 24 => get_string('numdays', '', 180), 150 * 3600 * 24 => get_string('numdays', '', 150), 120 * 3600 * 24 => get_string('numdays', '', 120), 90 * 3600 * 24 => get_string('numdays', '', 90), 60 * 3600 * 24 => get_string('numdays', '', 60), 30 * 3600 * 24 => get_string('numdays', '', 30), 21 * 3600 * 24 => get_string('numdays', '', 21), 14 * 3600 * 24 => get_string('numdays', '', 14), 7 * 3600 * 24 => get_string('numdays', '', 7)); $mform->addElement('select', 'customint2', get_string('longtimenosee', 'enrol_self'), $options); $mform->addHelpButton('customint2', 'longtimenosee', 'enrol_self'); $mform->addElement('text', 'customint3', get_string('maxenrolled', 'enrol_self')); $mform->addHelpButton('customint3', 'maxenrolled', 'enrol_self'); $mform->setType('customint3', PARAM_INT); $cohorts = array(0 => get_string('no')); $allcohorts = cohort_get_available_cohorts($context, 0, 0, 0); if ($instance->customint5 && !isset($allcohorts[$instance->customint5]) && ($c = $DB->get_record('cohort', array('id' => $instance->customint5), 'id, name, idnumber, contextid, visible', IGNORE_MISSING))) { // Current cohort was not found because current user can not see it. Still keep it. $allcohorts[$instance->customint5] = $c; } foreach ($allcohorts as $c) { $cohorts[$c->id] = format_string($c->name, true, array('context' => context::instance_by_id($c->contextid))); if ($c->idnumber) { $cohorts[$c->id] .= ' [' . s($c->idnumber) . ']'; } } if ($instance->customint5 && !isset($allcohorts[$instance->customint5])) { // Somebody deleted a cohort, better keep the wrong value so that random ppl can not enrol. $cohorts[$instance->customint5] = get_string('unknowncohort', 'cohort', $instance->customint5); } if (count($cohorts) > 1) { $mform->addElement('select', 'customint5', get_string('cohortonly', 'enrol_self'), $cohorts); $mform->addHelpButton('customint5', 'cohortonly', 'enrol_self'); } else { $mform->addElement('hidden', 'customint5'); $mform->setType('customint5', PARAM_INT); $mform->setConstant('customint5', 0); } $mform->addElement('advcheckbox', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self')); $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self'); $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'enrol_self'), array('cols' => '60', 'rows' => '8')); $mform->addHelpButton('customtext1', 'customwelcomemessage', 'enrol_self'); $mform->addElement('hidden', 'id'); $mform->setType('id', PARAM_INT); $mform->addElement('hidden', 'courseid'); $mform->setType('courseid', PARAM_INT); if (enrol_accessing_via_instance($instance)) { $mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), get_string('instanceeditselfwarningtext', 'core_enrol')); } $this->add_action_buttons(true, $instance->id ? null : get_string('addinstance', 'enrol')); $this->set_data($instance); }
/** * 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 $CFG; require_once $CFG->dirroot . '/cohort/lib.php'; $coursecontext = context_course::instance($courseid); if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/cohort:config', $coursecontext)) { return false; } return cohort_get_available_cohorts($coursecontext, 0, 0, 1) ? true : false; }
/** * Search cohorts. * TODO: MDL-52243 Move this function to cohorts/externallib.php * * @param string $query * @param array $context * @param string $includes * @param int $limitfrom * @param int $limitnum * @return array */ public static function search_cohorts($query, $context, $includes = 'parents', $limitfrom = 0, $limitnum = 25) { global $DB, $CFG, $PAGE; require_once $CFG->dirroot . '/cohort/lib.php'; $params = self::validate_parameters(self::search_cohorts_parameters(), array('query' => $query, 'context' => $context, 'includes' => $includes, 'limitfrom' => $limitfrom, 'limitnum' => $limitnum)); $query = $params['query']; $includes = $params['includes']; $context = self::get_context_from_params($params['context']); $limitfrom = $params['limitfrom']; $limitnum = $params['limitnum']; self::validate_context($context); $output = $PAGE->get_renderer('tool_lp'); $manager = has_capability('moodle/cohort:manage', $context); if (!$manager) { require_capability('moodle/cohort:view', $context); } // TODO Make this more efficient. if ($includes == 'self') { $results = cohort_get_cohorts($context->id, $limitfrom, $limitnum, $query); $results = $results['cohorts']; } else { if ($includes == 'parents') { $results = cohort_get_cohorts($context->id, $limitfrom, $limitnum, $query); $results = $results['cohorts']; if (!$context instanceof context_system) { $results = array_merge($results, cohort_get_available_cohorts($context, COHORT_ALL, $limitfrom, $limitnum, $query)); } } else { if ($includes == 'all') { $results = cohort_get_all_cohorts($limitfrom, $limitnum, $query); $results = $results['cohorts']; } else { throw new coding_exception('Invalid parameter value for \'includes\'.'); } } } $cohorts = array(); foreach ($results as $key => $cohort) { $cohortcontext = context::instance_by_id($cohort->contextid); $exporter = new cohort_summary_exporter($cohort, array('context' => $cohortcontext)); $newcohort = $exporter->export($output); $cohorts[$key] = $newcohort; } return array('cohorts' => $cohorts); }
/** * Return an array of valid options for the cohorts. * * @param stdClass $instance * @param context $context * @return array */ protected function get_cohort_options($instance, $context) { global $DB, $CFG; require_once $CFG->dirroot . '/cohort/lib.php'; $cohorts = array(); if ($instance->id) { if ($cohort = $DB->get_record('cohort', array('id' => $instance->customint1))) { $name = format_string($cohort->name, true, array('context' => context::instance_by_id($cohort->contextid))); $cohorts = array($instance->customint1 => $name); } else { $cohorts = array($instance->customint1 => get_string('error')); } } else { $cohorts = array('' => get_string('choosedots')); $allcohorts = cohort_get_available_cohorts($context, 0, 0, 0); foreach ($allcohorts as $c) { $cohorts[$c->id] = format_string($c->name); } } return $cohorts; }
/** * Definition of group creation form */ protected function definition() { global $CFG, $DB, $PAGE; $mform = $this->_form; $mform->addElement('hidden', 'id'); $mform->setDefault('id', $this->_customdata['id']); $mform->setType('id', PARAM_INT); $this->context = \context_module::instance($this->_customdata['id']); $cm = get_coursemodule_from_id('grouptool', $this->_customdata['id']); $course = $DB->get_record('course', array('id' => $cm->course)); $grouptool = $DB->get_record('grouptool', array('id' => $cm->instance), '*', MUST_EXIST); $coursecontext = \context_course::instance($cm->course); $mform->addElement('hidden', 'tab'); $mform->setDefault('tab', 'group_creation'); $mform->setType('tab', PARAM_TEXT); if (has_capability('mod/grouptool:create_groups', $this->context)) { /* ------------------------------------------------------------------------------- * Adding the "group creation" fieldset, where all the common settings are showed! */ $mform->addElement('header', 'group_creation', get_string('groupcreation', 'grouptool')); $options = array(0 => get_string('all')); $options += $this->_customdata['roles']; $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options); $student = get_archetype_roles('student'); $student = reset($student); if ($student and array_key_exists($student->id, $options)) { $mform->setDefault('roleid', $student->id); } $canviewcohorts = has_capability('moodle/cohort:view', $this->context); if ($canviewcohorts) { $cohorts = cohort_get_available_cohorts($coursecontext, true, 0, 0); if (count($cohorts) != 0) { $options = array(0 => get_string('anycohort', 'cohort')); foreach ($cohorts as $cohort) { $options[$cohort->id] = $cohort->name; } $mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'grouptool'), $options); $mform->setDefault('cohortid', '0'); } } else { $cohorts = array(); } if (!$canviewcohorts || count($cohorts) == 0) { $mform->addElement('hidden', 'cohortid'); $mform->setType('cohortid', PARAM_INT); $mform->setConstant('cohortid', '0'); } $mform->addElement('hidden', 'seed'); $mform->setType('seed', PARAM_INT); $radioarray = array(); $radioarray[] = $mform->createElement('radio', 'mode', '', get_string('define_amount_groups', 'grouptool'), GROUPTOOL_GROUPS_AMOUNT); $radioarray[] = $mform->createElement('radio', 'mode', '', get_string('define_amount_members', 'grouptool'), GROUPTOOL_MEMBERS_AMOUNT); $radioarray[] = $mform->createElement('radio', 'mode', '', get_string('create_1_person_groups', 'grouptool'), GROUPTOOL_1_PERSON_GROUPS); $radioarray[] = $mform->createElement('radio', 'mode', '', get_string('create_fromto_groups', 'grouptool'), GROUPTOOL_FROMTO_GROUPS); $mform->addGroup($radioarray, 'modearray', get_string('groupcreationmode', 'grouptool'), \html_writer::empty_tag('br'), false); $mform->setDefault('mode', GROUPTOOL_GROUPS_AMOUNT); $mform->addHelpButton('modearray', 'groupcreationmode', 'grouptool'); $mform->addElement('text', 'amount', get_string('group_or_member_count', 'grouptool'), array('size' => '4')); $mform->disabledif('amount', 'mode', 'eq', GROUPTOOL_1_PERSON_GROUPS); $mform->disabledif('amount', 'mode', 'eq', GROUPTOOL_FROMTO_GROUPS); /* * We have to clean this params by ourselves afterwards otherwise we get problems * with texts getting mapped to 0 */ $mform->setType('amount', PARAM_RAW); $mform->setDefault('amount', 2); $fromto = array(); $fromto[] = $mform->createElement('text', 'from', get_string('from')); $mform->setDefault('from', 0); /* * We have to clean this params by ourselves afterwards otherwise we get problems * with texts getting mapped to 0 */ $mform->setType('from', PARAM_RAW); $fromto[] = $mform->createElement('text', 'to', get_string('to')); $mform->setDefault('to', 0); /* * We have to clean this params by ourselves afterwards otherwise we get problems * with texts getting mapped to 0 */ $mform->setType('to', PARAM_RAW); $fromto[] = $mform->createElement('text', 'digits', get_string('digits', 'grouptool')); $mform->setDefault('digits', 2); /* * We have to clean this params by ourselves afterwards otherwise we get problems * with texts getting mapped to 0 */ $mform->setType('digits', PARAM_RAW); $mform->addGroup($fromto, 'fromto', get_string('groupfromtodigits', 'grouptool'), array(' ' . \html_writer::tag('label', '-', array('for' => 'id_from')) . ' ', ' ' . \html_writer::tag('label', get_string('digits', 'grouptool'), array('for' => 'id_digits')) . ' '), false); $mform->disabledif('from', 'mode', 'noteq', GROUPTOOL_FROMTO_GROUPS); $mform->disabledif('to', 'mode', 'noteq', GROUPTOOL_FROMTO_GROUPS); $mform->disabledif('digits', 'mode', 'noteq', GROUPTOOL_FROMTO_GROUPS); $mform->setAdvanced('fromto'); $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group')); $mform->addHelpButton('nosmallgroups', 'nosmallgroups', 'grouptool'); $mform->disabledif('nosmallgroups', 'mode', 'noteq', GROUPTOOL_MEMBERS_AMOUNT); $mform->disabledif('nosmallgroups', 'mode', 'eq', GROUPTOOL_FROMTO_GROUPS); $mform->setAdvanced('nosmallgroups'); $options = array('no' => get_string('noallocation', 'group'), 'random' => get_string('random', 'group'), 'firstname' => get_string('byfirstname', 'group'), 'lastname' => get_string('bylastname', 'group'), 'idnumber' => get_string('byidnumber', 'group')); $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options); if ($grouptool->allow_reg) { $mform->setDefault('allocateby', 'no'); } else { $mform->setDefault('allocateby', 'random'); } $mform->disabledif('allocateby', 'mode', 'eq', GROUPTOOL_1_PERSON_GROUPS); $mform->disabledif('allocateby', 'mode', 'eq', GROUPTOOL_FROMTO_GROUPS); $naminggrp = array(); $naminggrp[] =& $mform->createElement('text', 'namingscheme', '', array('size' => '64')); $naminggrp[] =& $mform->createElement('static', 'tags', '', get_string('name_scheme_tags', 'grouptool')); $namingstd = get_config('mod_grouptool', 'name_scheme'); $namingstd = !empty($namingstd) ? $namingstd : get_string('group', 'group') . ' #'; $mform->setDefault('namingscheme', $namingstd); $mform->setType('namingscheme', PARAM_RAW); $mform->addGroup($naminggrp, 'naminggrp', get_string('namingscheme', 'grouptool'), ' ', false); $mform->addHelpButton('naminggrp', 'namingscheme', 'grouptool'); // Init JS! $PAGE->requires->string_for_js('showmore', 'form'); $PAGE->requires->string_for_js('showless', 'form'); $PAGE->requires->yui_module('moodle-mod_grouptool-groupcreation', 'M.mod_grouptool.init_groupcreation', array(array('fromto_mode' => GROUPTOOL_FROMTO_GROUPS))); $selectgroups = $mform->createElement('selectgroups', 'grouping', get_string('createingrouping', 'group')); $options = array('0' => get_string('no')); if (has_capability('mod/grouptool:create_groupings', $this->context)) { $options['-1'] = get_string('onenewgrouping', 'grouptool'); } $selectgroups->addOptGroup("", $options); if ($groupings = groups_get_all_groupings($course->id)) { $options = array(); foreach ($groupings as $grouping) { $options[$grouping->id] = strip_tags(format_string($grouping->name)); } $selectgroups->addOptGroup("————————————————————————", $options); } $mform->addElement($selectgroups); if ($groupings) { $mform->setDefault('grouping', '0'); } if (has_capability('mod/grouptool:create_groupings', $this->context)) { $mform->addElement('text', 'groupingname', get_string('groupingname', 'group')); $mform->setType('groupingname', PARAM_MULTILANG); $mform->disabledif('groupingname', 'grouping', 'noteq', '-1'); } $mform->addElement('submit', 'createGroups', get_string('createGroups', 'grouptool')); } }