require_capability('moodle/cohort:view', $context); } $strcohorts = get_string('cohorts', 'cohort'); if ($category) { $PAGE->set_pagelayout('admin'); $PAGE->set_context($context); $PAGE->set_url('/cohort/index.php', array('contextid' => $context->id)); $PAGE->set_title($strcohorts); $PAGE->set_heading($COURSE->fullname); $showall = false; } else { admin_externalpage_setup('cohorts', '', null, '', array('pagelayout' => 'report')); } echo $OUTPUT->header(); if ($showall) { $cohorts = cohort_get_all_cohorts($page, 25, $searchquery); } else { $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); $params = array('page' => $page); if ($contextid) { $params['contextid'] = $contextid;
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']); }
/** * 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); }