function definition() { $mform = $this->_form; // course $courses = array(); $dbcourses = get_courses(); foreach ($dbcourses as $dbcourseid => $dbcourse) { if ($dbcourse->id > 1) { // 1 = system course, ala front-page. probably don't want it there! $courses[$dbcourseid] = $dbcourse->fullname; } } $mform->addElement('select', 'course', get_string('promptcourse', 'block_enrol_token_manager'), $courses); $mform->addHelpButton('course', 'promptcourse', 'block_enrol_token_manager'); // cohorts $context = context_system::instance(); $cohorts = array(); $dbcohorts = cohort_get_cohorts($context->id); foreach ($dbcohorts['cohorts'] as $dbcohort) { $cohorts[$dbcohort->id] = $dbcohort->name; } // cohort selection $mform->addElement('header', 'cohorts', get_string('cohort_selection', 'block_enrol_token_manager')); $mform->addElement('select', 'cohortexisting', get_string('promptcohortexisting', 'block_enrol_token_manager'), $cohorts); $mform->addElement('static', 'cohortor', '', 'OR'); $mform->addHelpButton('cohortor', 'cohortor', 'block_enrol_token_manager'); $mform->addElement('text', 'cohortnew', get_string('promptcohortnew', 'block_enrol_token_manager'), 'maxlength="253" size="25"'); $mform->setType('cohortnew', PARAM_CLEANHTML); // token prefix $mform->addElement('header', 'tokens', get_string('token_generation', 'block_enrol_token_manager')); $mform->addElement('text', 'prefix', get_string('promptprefix', 'block_enrol_token_manager'), 'maxlength="4" size="4"'); $mform->addHelpButton('prefix', 'promptprefix_help', 'block_enrol_token_manager'); // $mform->addElement('static', 'prefixinstructions', get_string('prefixinstructions', 'block_enrol_token_manager')); $mform->setType('prefix', PARAM_ALPHANUMEXT); // seats per token $mform->addElement('text', 'seatspertoken', get_string('promptseats', 'block_enrol_token_manager'), 'maxlength="3" size="3"'); $mform->addHelpButton('seatspertoken', 'promptseats', 'block_enrol_token_manager'); $mform->setType('seatspertoken', PARAM_INT); // number of tokens $mform->addElement('text', 'tokennumber', get_string('prompttokennum', 'block_enrol_token_manager'), 'maxlength="3" size="3"'); $mform->addHelpButton('tokennumber', 'prompttokennum', 'block_enrol_token_manager'); $mform->setType('tokennumber', PARAM_INT); // expiry date $mform->addElement('date_selector', 'expirydate', get_string('promptexpirydate', 'block_enrol_token_manager'), array('optional' => true)); // email to $mform->addElement('text', 'emailaddress', get_string('emailaddressprompt', 'block_enrol_token_manager'), 'maxlength="128" size="50"'); $mform->setType('emailaddress', PARAM_EMAIL); // buttons $this->add_action_buttons(false, get_string('createtokens', 'block_enrol_token_manager')); }
$canassign = has_capability('moodle/cohort:assign', $context); if (!$manager) { require_capability('moodle/cohort:view', $context); } $strcohorts = get_string('cohorts', 'cohort'); if ($category) { $PAGE->set_pagelayout('report'); $PAGE->set_context($context); $PAGE->set_url('/cohort/index.php', array('contextid' => $context->id)); $PAGE->set_title($strcohorts); $PAGE->set_heading($COURSE->fullname); } else { admin_externalpage_setup('cohorts', '', null, '', array('pagelayout' => 'report')); } echo $OUTPUT->header(); $cohorts = cohort_get_cohorts($context->id, $page, 25, $searchquery); $count = ''; if ($cohorts['allcohorts'] > 0) { if ($searchquery === '') { $count = ' (' . $cohorts['allcohorts'] . ')'; } else { $count = ' (' . $cohorts['totalcohorts'] . '/' . $cohorts['allcohorts'] . ')'; } } echo $OUTPUT->heading(get_string('cohortsin', 'cohort', $context->get_context_name()) . $count); // Add search form. $search = html_writer::start_tag('form', array('id' => 'searchcohortquery', 'method' => 'get')); $search .= html_writer::start_tag('div'); $search .= html_writer::label(get_string('searchcohort', 'cohort'), 'cohort_search_q'); // No : in form labels! $search .= html_writer::empty_tag('input', array('id' => 'cohort_search_q', 'type' => 'text', 'name' => 'search', 'value' => $searchquery));
public function test_cohort_get_all_cohorts() { global $DB; $this->resetAfterTest(); $category1 = $this->getDataGenerator()->create_category(); $category2 = $this->getDataGenerator()->create_category(); $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($category1->id)->id, 'name' => 'aaagrrryyy', 'idnumber' => '', 'description' => '')); $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($category1->id)->id, 'name' => 'bbb', 'idnumber' => '', 'description' => 'yyybrrr')); $cohort3 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($category2->id)->id, 'name' => 'ccc', 'idnumber' => 'xxarrrghyyy', 'description' => 'po_us')); $cohort4 = $this->getDataGenerator()->create_cohort(array('contextid' => context_system::instance()->id)); // Get list of all cohorts as admin. $this->setAdminUser(); $result = cohort_get_all_cohorts(0, 100, ''); $this->assertEquals(4, $result['totalcohorts']); $this->assertEquals(array($cohort1->id => $cohort1, $cohort2->id => $cohort2, $cohort3->id => $cohort3, $cohort4->id => $cohort4), $result['cohorts']); $this->assertEquals(4, $result['allcohorts']); $result = cohort_get_all_cohorts(0, 100, 'grrr'); $this->assertEquals(1, $result['totalcohorts']); $this->assertEquals(array($cohort1->id => $cohort1), $result['cohorts']); $this->assertEquals(4, $result['allcohorts']); // Get list of all cohorts as manager who has capability everywhere. $user = $this->getDataGenerator()->create_user(); $managerrole = $DB->get_record('role', array('shortname' => 'manager')); role_assign($managerrole->id, $user->id, context_system::instance()->id); $this->setUser($user); $result = cohort_get_all_cohorts(0, 100, ''); $this->assertEquals(4, $result['totalcohorts']); $this->assertEquals(array($cohort1->id => $cohort1, $cohort2->id => $cohort2, $cohort3->id => $cohort3, $cohort4->id => $cohort4), $result['cohorts']); $this->assertEquals(4, $result['allcohorts']); $result = cohort_get_all_cohorts(0, 100, 'grrr'); $this->assertEquals(1, $result['totalcohorts']); $this->assertEquals(array($cohort1->id => $cohort1), $result['cohorts']); $this->assertEquals(4, $result['allcohorts']); // Get list of all cohorts as manager who has capability everywhere except category2. $context2 = context_coursecat::instance($category2->id); role_change_permission($managerrole->id, $context2, 'moodle/cohort:view', CAP_PROHIBIT); role_change_permission($managerrole->id, $context2, 'moodle/cohort:manage', CAP_PROHIBIT); $this->assertFalse(has_any_capability(array('moodle/cohort:view', 'moodle/cohort:manage'), $context2)); $result = cohort_get_all_cohorts(0, 100, ''); $this->assertEquals(3, $result['totalcohorts']); $this->assertEquals(array($cohort1->id => $cohort1, $cohort2->id => $cohort2, $cohort4->id => $cohort4), $result['cohorts']); $this->assertEquals(3, $result['allcohorts']); $result = cohort_get_all_cohorts(0, 100, 'grrr'); $this->assertEquals(1, $result['totalcohorts']); $this->assertEquals(array($cohort1->id => $cohort1), $result['cohorts']); $this->assertEquals(3, $result['allcohorts']); $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 1, 1, 'yyy'); $this->assertEquals(2, $result['totalcohorts']); $this->assertEquals(array($cohort2->id => $cohort2), $result['cohorts']); $this->assertEquals(2, $result['allcohorts']); }
public function test_cohort_get_cohorts() { global $DB; $this->resetAfterTest(); $category1 = $this->getDataGenerator()->create_category(); $category2 = $this->getDataGenerator()->create_category(); $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($category1->id)->id, 'name' => 'aaagrrryyy', 'idnumber' => '', 'description' => '')); $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($category1->id)->id, 'name' => 'bbb', 'idnumber' => '', 'description' => 'yyybrrr')); $cohort3 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($category1->id)->id, 'name' => 'ccc', 'idnumber' => 'xxarrrghyyy', 'description' => 'po_us')); $cohort4 = $this->getDataGenerator()->create_cohort(array('contextid' => context_system::instance()->id)); $result = cohort_get_cohorts(context_coursecat::instance($category2->id)->id); $this->assertEquals(0, $result['totalcohorts']); $this->assertEquals(0, count($result['cohorts'])); $this->assertEquals(0, $result['allcohorts']); $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id); $this->assertEquals(3, $result['totalcohorts']); $this->assertEquals(array($cohort1->id => $cohort1, $cohort2->id => $cohort2, $cohort3->id => $cohort3), $result['cohorts']); $this->assertEquals(3, $result['allcohorts']); $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'arrrgh'); $this->assertEquals(1, $result['totalcohorts']); $this->assertEquals(array($cohort3->id => $cohort3), $result['cohorts']); $this->assertEquals(3, $result['allcohorts']); $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'brrr'); $this->assertEquals(1, $result['totalcohorts']); $this->assertEquals(array($cohort2->id => $cohort2), $result['cohorts']); $this->assertEquals(3, $result['allcohorts']); $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'grrr'); $this->assertEquals(1, $result['totalcohorts']); $this->assertEquals(array($cohort1->id => $cohort1), $result['cohorts']); $this->assertEquals(3, $result['allcohorts']); $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 1, 1, 'yyy'); $this->assertEquals(3, $result['totalcohorts']); $this->assertEquals(array($cohort2->id => $cohort2), $result['cohorts']); $this->assertEquals(3, $result['allcohorts']); $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'po_us'); $this->assertEquals(1, $result['totalcohorts']); $this->assertEquals(array($cohort3->id => $cohort3), $result['cohorts']); $this->assertEquals(3, $result['allcohorts']); $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'pokus'); $this->assertEquals(0, $result['totalcohorts']); $this->assertEquals(array(), $result['cohorts']); $this->assertEquals(3, $result['allcohorts']); }
/** * 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); }