Merge branch 'w38_MDL-35465_m24_cohortscleanup' of git://github.com/skodak/moodle

This commit is contained in:
Eloy Lafuente (stronk7)
2012-09-18 16:55:42 +02:00
10 changed files with 615 additions and 206 deletions
+5 -10
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -18,14 +17,13 @@
/**
* Cohort related management functions, this file needs to be included manually.
*
* @package core
* @subpackage cohort
* @package core_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../config.php');
require_once($CFG->dirroot.'/cohort/lib.php');
require('../config.php');
require_once($CFG->dirroot.'/cohort/locallib.php');
$id = required_param('id', PARAM_INT);
@@ -42,7 +40,7 @@ $PAGE->set_url('/cohort/assign.php', array('id'=>$id));
$returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid));
if (!empty($cohort->component)) {
// we can not manually edit cohorts that were created by external systems, sorry
// We can not manually edit cohorts that were created by external systems, sorry.
redirect($returnurl);
}
@@ -80,10 +78,7 @@ if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
if (!empty($userstoassign)) {
foreach ($userstoassign as $adduser) {
// no duplicates please
if (!$DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$adduser->id))) {
cohort_add_member($cohort->id, $adduser->id);
}
cohort_add_member($cohort->id, $adduser->id);
}
$potentialuserselector->invalidate_selected_users();
+5 -8
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -15,12 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Cohort related management functions, this file needs to be included manually.
*
* @package core
* @subpackage cohort
* @package core_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@@ -58,7 +55,7 @@ require_capability('moodle/cohort:manage', $context);
$returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id));
if (!empty($cohort->component)) {
// we can not manually edit cohorts that were created by external systems, sorry
// We can not manually edit cohorts that were created by external systems, sorry.
redirect($returnurl);
}
@@ -97,12 +94,12 @@ if ($delete and $cohort->id) {
$editoroptions = array('maxfiles'=>0, 'context'=>$context);
if ($cohort->id) {
// edit existing
// Edit existing.
$cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
$strheading = get_string('editcohort', 'cohort');
} else {
// add new
// Add new.
$cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
$strheading = get_string('addcohort', 'cohort');
}
@@ -125,7 +122,7 @@ if ($editform->is_cancelled()) {
cohort_add_cohort($data);
}
// use new context id, it could have been changed
// Use new context id, it could have been changed.
redirect(new moodle_url('/cohort/index.php', array('contextid'=>$data->contextid)));
}
+7 -11
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -18,15 +17,12 @@
/**
* Cohort related management functions, this file needs to be included manually.
*
* @package core
* @subpackage cohort
* @package core_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/lib/formslib.php');
@@ -49,7 +45,7 @@ class cohort_edit_form extends moodleform {
$mform->addElement('select', 'contextid', get_string('context', 'role'), $options);
$mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
$mform->setType('idnumber', PARAM_RAW); // idnumbers are plain text, must not be changed
$mform->setType('idnumber', PARAM_RAW); // Idnumbers are plain text, must not be changed.
$mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions);
$mform->setType('description_editor', PARAM_RAW);
@@ -69,7 +65,7 @@ class cohort_edit_form extends moodleform {
$idnumber = trim($data['idnumber']);
if ($idnumber === '') {
// fine, empty is ok
// Fine, empty is ok.
} else if ($data['id']) {
$current = $DB->get_record('cohort', array('id'=>$data['id']), '*', MUST_EXIST);
@@ -95,16 +91,16 @@ class cohort_edit_form extends moodleform {
$options = array();
$syscontext = context_system::instance();
if (has_capability('moodle/cohort:manage', $syscontext)) {
$options[$syscontext->id] = print_context_name($syscontext);
$options[$syscontext->id] = $syscontext->get_context_name();
}
foreach ($displaylist as $cid=>$name) {
$context = context_coursecat::instance($cid);
$options[$context->id] = $name;
}
// always add current - this is not likely, but if the logic gets changed it might be a problem
// Always add current - this is not likely, but if the logic gets changed it might be a problem.
if (!isset($options[$currentcontextid])) {
$context = context::instance_by_id($currentcontextid, MUST_EXIST);
$options[$context->id] = print_context_name($syscontext);
$options[$context->id] = $syscontext->get_context_name();
}
return $options;
}
+18 -10
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -18,8 +17,7 @@
/**
* Cohort related management functions, this file needs to be included manually.
*
* @package core
* @subpackage cohort
* @package core_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@@ -69,21 +67,31 @@ if ($category) {
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('cohortsin', 'cohort', print_context_name($context)));
$cohorts = cohort_get_cohorts($context->id, $page, 25, $searchquery);
// add search form
$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');
$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));
$search .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('search', 'cohort')));
$search .= html_writer::end_tag('div');
$search .= html_writer::end_tag('form');
echo $search;
$cohorts = cohort_get_cohorts($context->id, $page, 25, $searchquery);
// output pagination bar
// Output pagination bar.
$params = array('page' => $page);
if ($contextid) {
$params['contextid'] = $contextid;
@@ -98,7 +106,7 @@ $data = array();
foreach($cohorts['cohorts'] as $cohort) {
$line = array();
$line[] = format_string($cohort->name);
$line[] = s($cohort->idnumber); // plain text
$line[] = s($cohort->idnumber); // All idnumbers are plain text.
$line[] = format_text($cohort->description, $cohort->descriptionformat);
$line[] = $DB->count_records('cohort_members', array('cohortid'=>$cohort->id));
@@ -137,4 +145,4 @@ if ($manager) {
echo $OUTPUT->single_button(new moodle_url('/cohort/edit.php', array('contextid'=>$context->id)), get_string('add'));
}
echo $OUTPUT->footer();
echo $OUTPUT->footer();
+50 -165
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -18,19 +17,18 @@
/**
* Cohort related management functions, this file needs to be included manually.
*
* @package core
* @subpackage cohort
* @package core_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/user/selector/lib.php');
defined('MOODLE_INTERNAL') || die();
/**
* Add new cohort.
*
* @param object $cohort
* @return int
* @param stdClass $cohort
* @return int new cohort id
*/
function cohort_add_cohort($cohort) {
global $DB;
@@ -42,7 +40,8 @@ function cohort_add_cohort($cohort) {
$cohort->idnumber = NULL;
}
if (!isset($cohort->description)) {
$cohort->description = $DB->sql_empty();
// sql_empty() does not belong here, this crazy Oracle hack is implemented in insert_record()!
$cohort->description = '';
}
if (!isset($cohort->descriptionformat)) {
$cohort->descriptionformat = FORMAT_HTML;
@@ -66,7 +65,7 @@ function cohort_add_cohort($cohort) {
/**
* Update existing cohort.
* @param object $cohort
* @param stdClass $cohort
* @return void
*/
function cohort_update_cohort($cohort) {
@@ -83,7 +82,7 @@ function cohort_update_cohort($cohort) {
/**
* Delete cohort.
* @param object $cohort
* @param stdClass $cohort
* @return void
*/
function cohort_delete_cohort($cohort) {
@@ -103,7 +102,7 @@ function cohort_delete_cohort($cohort) {
* Somehow deal with cohorts when deleting course category,
* we can not just delete them because they might be used in enrol
* plugins or referenced in external systems.
* @param object $category
* @param stdClass $category
* @return void
*/
function cohort_delete_category($category) {
@@ -133,6 +132,10 @@ function cohort_delete_category($category) {
*/
function cohort_add_member($cohortid, $userid) {
global $DB;
if ($DB->record_exists('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid))) {
// No duplicates!
return;
}
$record = new stdClass();
$record->cohortid = $cohortid;
$record->userid = $userid;
@@ -168,34 +171,46 @@ function cohort_is_member($cohortid, $userid) {
}
/**
* Returns list of visible cohorts in course.
* Returns list of cohorts from course parent contexts.
*
* @param object $course
* @param bool $enrolled true means include only cohorts with enrolled users
* @return array
* Note: this function does not implement any capability checks,
* it means it may disclose existence of cohorts,
* make sure it is displayed to users with appropriate rights only.
*
* @param stdClass $course
* @param bool $onlyenrolled true means include only cohorts with enrolled users
* @return array of cohort names with number of enrolled users
*/
function cohort_get_visible_list($course) {
global $DB, $USER;
function cohort_get_visible_list($course, $onlyenrolled=true) {
global $DB;
$context = context_course::instance($course->id);
list($esql, $params) = get_enrolled_sql($context);
$parentsql = get_related_contexts_string($context);
list($parentsql, $params2) = $DB->get_in_or_equal($context->get_parent_context_ids(), SQL_PARAMS_NAMED);
$params = array_merge($params, $params2);
$sql = "SELECT c.id, c.name, c.idnumber, COUNT(u.id) AS cnt
if ($onlyenrolled) {
$left = "";
$having = "HAVING COUNT(u.id) > 0";
} else {
$left = "LEFT";
$having = "";
}
$sql = "SELECT c.id, c.name, c.contextid, c.idnumber, COUNT(u.id) AS cnt
FROM {cohort} c
JOIN {cohort_members} cm ON cm.cohortid = c.id
JOIN ($esql) u ON u.id = cm.userid
$left JOIN ({cohort_members} cm
JOIN ($esql) u ON u.id = cm.userid) ON cm.cohortid = c.id
WHERE c.contextid $parentsql
GROUP BY c.id, c.name, c.idnumber
HAVING COUNT(u.id) > 0
GROUP BY c.id, c.name, c.contextid, c.idnumber
$having
ORDER BY c.name, c.idnumber";
$params['ctx'] = $context->id;
$cohorts = $DB->get_records_sql($sql, $params);
foreach ($cohorts as $cid=>$cohort) {
$cohorts[$cid] = format_string($cohort->name);
if ($cohort->idnumber) {
$cohorts[$cid] = format_string($cohort->name, true, array('context'=>$cohort->contextid));
if ($cohort->cnt) {
$cohorts[$cid] .= ' (' . $cohort->cnt . ')';
}
}
@@ -204,170 +219,40 @@ function cohort_get_visible_list($course) {
}
/**
* Get all the cohorts.
* Get all the cohorts defined in given context.
*
* @global moodle_database $DB
* @param int $contextid
* @param int $page number of the current page
* @param int $perpage items per page
* @param string $search search string
* @return array Array(totalcohorts => int, cohorts => array)
* @return array Array(totalcohorts => int, cohorts => array, allcohorts => int)
*/
function cohort_get_cohorts($contextid, $page = 0, $perpage = 25, $search = '') {
global $DB;
$cohorts = array();
// Add some additional sensible conditions
$tests = array('contextid = ?');
$params = array($contextid);
if (!empty($search)) {
$conditions = array(
'name',
'idnumber',
'description',
);
$searchparam = '%' . $search . '%';
$conditions = array('name', 'idnumber', 'description');
$searchparam = '%' . $DB->sql_like_escape($search) . '%';
foreach ($conditions as $key=>$condition) {
$conditions[$key] = $DB->sql_like($condition,"?", false);
$conditions[$key] = $DB->sql_like($condition, "?", false);
$params[] = $searchparam;
}
$tests[] = '(' . implode(' OR ', $conditions) . ')';
}
$wherecondition = implode(' AND ', $tests);
$fields = 'SELECT *';
$countfields = 'SELECT COUNT(1)';
$fields = "SELECT *";
$countfields = "SELECT COUNT(1)";
$sql = " FROM {cohort}
WHERE $wherecondition";
$order = ' ORDER BY name ASC';
$order = " ORDER BY name ASC, idnumber ASC";
$allcohorts = $DB->count_records('cohort', array('contextid'=>$contextid));
$totalcohorts = $DB->count_records_sql($countfields . $sql, $params);
$cohorts = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage);
return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts);
}
/**
* Cohort assignment candidates
*/
class cohort_candidate_selector extends user_selector_base {
protected $cohortid;
public function __construct($name, $options) {
$this->cohortid = $options['cohortid'];
parent::__construct($name, $options);
}
/**
* Candidate users
* @param <type> $search
* @return array
*/
public function find_users($search) {
global $DB;
//by default wherecondition retrieves all users except the deleted, not confirmed and guest
list($wherecondition, $params) = $this->search_sql($search, 'u');
$params['cohortid'] = $this->cohortid;
$fields = 'SELECT ' . $this->required_fields_sql('u');
$countfields = 'SELECT COUNT(1)';
$sql = " FROM {user} u
LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
WHERE cm.id IS NULL AND $wherecondition";
$order = ' ORDER BY u.lastname ASC, u.firstname ASC';
if (!$this->is_validating()) {
$potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
if ($potentialmemberscount > 100) {
return $this->too_many_results($search, $potentialmemberscount);
}
}
$availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
if (empty($availableusers)) {
return array();
}
if ($search) {
$groupname = get_string('potusersmatching', 'cohort', $search);
} else {
$groupname = get_string('potusers', 'cohort');
}
return array($groupname => $availableusers);
}
protected function get_options() {
$options = parent::get_options();
$options['cohortid'] = $this->cohortid;
$options['file'] = 'cohort/lib.php';
return $options;
}
}
/**
* Cohort assignment candidates
*/
class cohort_existing_selector extends user_selector_base {
protected $cohortid;
public function __construct($name, $options) {
$this->cohortid = $options['cohortid'];
parent::__construct($name, $options);
}
/**
* Candidate users
* @param <type> $search
* @return array
*/
public function find_users($search) {
global $DB;
//by default wherecondition retrieves all users except the deleted, not confirmed and guest
list($wherecondition, $params) = $this->search_sql($search, 'u');
$params['cohortid'] = $this->cohortid;
$fields = 'SELECT ' . $this->required_fields_sql('u');
$countfields = 'SELECT COUNT(1)';
$sql = " FROM {user} u
JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
WHERE $wherecondition";
$order = ' ORDER BY u.lastname ASC, u.firstname ASC';
if (!$this->is_validating()) {
$potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
if ($potentialmemberscount > 100) {
return $this->too_many_results($search, $potentialmemberscount);
}
}
$availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
if (empty($availableusers)) {
return array();
}
if ($search) {
$groupname = get_string('currentusersmatching', 'cohort', $search);
} else {
$groupname = get_string('currentusers', 'cohort');
}
return array($groupname => $availableusers);
}
protected function get_options() {
$options = parent::get_options();
$options['cohortid'] = $this->cohortid;
$options['file'] = 'cohort/lib.php';
return $options;
}
return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts, 'allcohorts'=>$allcohorts);
}
+155
View File
@@ -0,0 +1,155 @@
<?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/>.
/**
* Cohort UI related functions and classes.
*
* @package core_cohort
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/cohort/lib.php');
require_once($CFG->dirroot . '/user/selector/lib.php');
/**
* Cohort assignment candidates
*/
class cohort_candidate_selector extends user_selector_base {
protected $cohortid;
public function __construct($name, $options) {
$this->cohortid = $options['cohortid'];
parent::__construct($name, $options);
}
/**
* Candidate users
* @param string $search
* @return array
*/
public function find_users($search) {
global $DB;
// By default wherecondition retrieves all users except the deleted, not confirmed and guest.
list($wherecondition, $params) = $this->search_sql($search, 'u');
$params['cohortid'] = $this->cohortid;
$fields = 'SELECT ' . $this->required_fields_sql('u');
$countfields = 'SELECT COUNT(1)';
$sql = " FROM {user} u
LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
WHERE cm.id IS NULL AND $wherecondition";
$order = ' ORDER BY u.lastname ASC, u.firstname ASC';
if (!$this->is_validating()) {
$potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
if ($potentialmemberscount > 100) {
return $this->too_many_results($search, $potentialmemberscount);
}
}
$availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
if (empty($availableusers)) {
return array();
}
if ($search) {
$groupname = get_string('potusersmatching', 'cohort', $search);
} else {
$groupname = get_string('potusers', 'cohort');
}
return array($groupname => $availableusers);
}
protected function get_options() {
$options = parent::get_options();
$options['cohortid'] = $this->cohortid;
$options['file'] = 'cohort/locallib.php';
return $options;
}
}
/**
* Cohort assignment candidates
*/
class cohort_existing_selector extends user_selector_base {
protected $cohortid;
public function __construct($name, $options) {
$this->cohortid = $options['cohortid'];
parent::__construct($name, $options);
}
/**
* Candidate users
* @param string $search
* @return array
*/
public function find_users($search) {
global $DB;
// By default wherecondition retrieves all users except the deleted, not confirmed and guest.
list($wherecondition, $params) = $this->search_sql($search, 'u');
$params['cohortid'] = $this->cohortid;
$fields = 'SELECT ' . $this->required_fields_sql('u');
$countfields = 'SELECT COUNT(1)';
$sql = " FROM {user} u
JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
WHERE $wherecondition";
$order = ' ORDER BY u.lastname ASC, u.firstname ASC';
if (!$this->is_validating()) {
$potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
if ($potentialmemberscount > 100) {
return $this->too_many_results($search, $potentialmemberscount);
}
}
$availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
if (empty($availableusers)) {
return array();
}
if ($search) {
$groupname = get_string('currentusersmatching', 'cohort', $search);
} else {
$groupname = get_string('currentusers', 'cohort');
}
return array($groupname => $availableusers);
}
protected function get_options() {
$options = parent::get_options();
$options['cohortid'] = $this->cohortid;
$options['file'] = 'cohort/locallib.php';
return $options;
}
}
+313
View File
@@ -0,0 +1,313 @@
<?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/>.
/**
* Cohort library tests.
*
* @package core_cohort
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("$CFG->dirroot/cohort/lib.php");
/**
* Cohort library tests.
*
* @package core_cohort
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cohort_testcase extends advanced_testcase {
public function test_cohort_add_cohort() {
global $DB;
$this->resetAfterTest();
$cohort = new stdClass();
$cohort->contextid = context_system::instance()->id;
$cohort->name = 'test cohort';
$cohort->idnumber = 'testid';
$cohort->description = 'test cohort desc';
$cohort->descriptionformat = FORMAT_HTML;
$id = cohort_add_cohort($cohort);
$this->assertNotEmpty($id);
$newcohort = $DB->get_record('cohort', array('id'=>$id));
$this->assertEquals($cohort->contextid, $newcohort->contextid);
$this->assertSame($cohort->name, $newcohort->name);
$this->assertSame($cohort->description, $newcohort->description);
$this->assertEquals($cohort->descriptionformat, $newcohort->descriptionformat);
$this->assertNotEmpty($newcohort->timecreated);
$this->assertSame($newcohort->component, '');
$this->assertSame($newcohort->timecreated, $newcohort->timemodified);
try {
$cohort = new stdClass();
$cohort->contextid = context_system::instance()->id;
$cohort->name = null;
$cohort->idnumber = 'testid';
$cohort->description = 'test cohort desc';
$cohort->descriptionformat = FORMAT_HTML;
cohort_add_cohort($cohort);
$this->fail('Exception expected when trying to add cohort without name');
} catch (Exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
}
public function test_cohort_update_cohort() {
global $DB;
$this->resetAfterTest();
$cohort = new stdClass();
$cohort->contextid = context_system::instance()->id;
$cohort->name = 'test cohort';
$cohort->idnumber = 'testid';
$cohort->description = 'test cohort desc';
$cohort->descriptionformat = FORMAT_HTML;
$id = cohort_add_cohort($cohort);
$this->assertNotEmpty($id);
$DB->set_field('cohort', 'timecreated', $cohort->timecreated - 10, array('id'=>$id));
$DB->set_field('cohort', 'timemodified', $cohort->timemodified - 10, array('id'=>$id));
$cohort = $DB->get_record('cohort', array('id'=>$id));
$cohort->name = 'test cohort 2';
cohort_update_cohort($cohort);
$newcohort = $DB->get_record('cohort', array('id'=>$id));
$this->assertSame($cohort->contextid, $newcohort->contextid);
$this->assertSame($cohort->name, $newcohort->name);
$this->assertSame($cohort->description, $newcohort->description);
$this->assertSame($cohort->descriptionformat, $newcohort->descriptionformat);
$this->assertSame($cohort->timecreated, $newcohort->timecreated);
$this->assertSame($cohort->component, $newcohort->component);
$this->assertGreaterThan($newcohort->timecreated, $newcohort->timemodified);
$this->assertLessThanOrEqual(time(), $newcohort->timemodified);
}
public function test_cohort_delete_cohort() {
global $DB;
$this->resetAfterTest();
$cohort = $this->getDataGenerator()->create_cohort();
cohort_delete_cohort($cohort);
$this->assertFalse($DB->record_exists('cohort', array('id'=>$cohort->id)));
}
public function test_cohort_delete_category() {
global $DB;
$this->resetAfterTest();
$category = $this->getDataGenerator()->create_category();
$cohort = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category->id)->id));
cohort_delete_category($category);
$this->assertTrue($DB->record_exists('cohort', array('id'=>$cohort->id)));
$newcohort = $DB->get_record('cohort', array('id'=>$cohort->id));
$this->assertEquals(context_system::instance()->id, $newcohort->contextid);
}
public function test_cohort_add_member() {
global $DB;
$this->resetAfterTest();
$cohort = $this->getDataGenerator()->create_cohort();
$user = $this->getDataGenerator()->create_user();
$this->assertFalse($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
cohort_add_member($cohort->id, $user->id);
$this->assertTrue($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
}
public function test_cohort_remove_member() {
global $DB;
$this->resetAfterTest();
$cohort = $this->getDataGenerator()->create_cohort();
$user = $this->getDataGenerator()->create_user();
cohort_add_member($cohort->id, $user->id);
$this->assertTrue($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
cohort_remove_member($cohort->id, $user->id);
$this->assertFalse($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
}
public function test_cohort_is_member() {
global $DB;
$this->resetAfterTest();
$cohort = $this->getDataGenerator()->create_cohort();
$user = $this->getDataGenerator()->create_user();
$this->assertFalse(cohort_is_member($cohort->id, $user->id));
cohort_add_member($cohort->id, $user->id);
$this->assertTrue(cohort_is_member($cohort->id, $user->id));
}
public function test_cohort_get_visible_list() {
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));
$course3 = $this->getDataGenerator()->create_course();
$cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id));
$cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category2->id)->id));
$cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_system::instance()->id));
$cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_system::instance()->id));
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$user4 = $this->getDataGenerator()->create_user();
$user5 = $this->getDataGenerator()->create_user();
$manualenrol = enrol_get_plugin('manual');
$enrol1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'));
$enrol2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'));
$manualenrol->enrol_user($enrol1, $user1->id);
$manualenrol->enrol_user($enrol1, $user3->id);
$manualenrol->enrol_user($enrol1, $user4->id);
$manualenrol->enrol_user($enrol2, $user2->id);
cohort_add_member($cohort1->id, $user1->id);
cohort_add_member($cohort3->id, $user1->id);
cohort_add_member($cohort1->id, $user3->id);
cohort_add_member($cohort2->id, $user2->id);
$list = cohort_get_visible_list($course1);
$this->assertEquals(2, count($list));
$this->assertNotEmpty($list[$cohort1->id]);
$this->assertRegExp('/\(2\)$/', $list[$cohort1->id]);
$this->assertNotEmpty($list[$cohort3->id]);
$this->assertRegExp('/\(1\)$/', $list[$cohort3->id]);
$list = cohort_get_visible_list($course1, false);
$this->assertEquals(3, count($list));
$this->assertNotEmpty($list[$cohort1->id]);
$this->assertRegExp('/\(2\)$/', $list[$cohort1->id]);
$this->assertNotEmpty($list[$cohort3->id]);
$this->assertRegExp('/\(1\)$/', $list[$cohort3->id]);
$this->assertNotEmpty($list[$cohort4->id]);
$this->assertRegExp('/[^\)]$/', $list[$cohort4->id]);
$list = cohort_get_visible_list($course2);
$this->assertEquals(1, count($list));
$this->assertNotEmpty($list[$cohort2->id]);
$this->assertRegExp('/\(1\)$/', $list[$cohort2->id]);
$list = cohort_get_visible_list($course2, false);
$this->assertEquals(3, count($list));
$this->assertNotEmpty($list[$cohort2->id]);
$this->assertRegExp('/\(1\)$/', $list[$cohort2->id]);
$this->assertNotEmpty($list[$cohort3->id]);
$this->assertRegExp('/[^\)]$/', $list[$cohort3->id]);
$this->assertNotEmpty($list[$cohort4->id]);
$this->assertRegExp('/[^\)]$/', $list[$cohort4->id]);
$list = cohort_get_visible_list($course3);
$this->assertEquals(0, count($list));
$list = cohort_get_visible_list($course3, false);
$this->assertEquals(2, count($list));
$this->assertNotEmpty($list[$cohort3->id]);
$this->assertRegExp('/[^\)]$/', $list[$cohort3->id]);
$this->assertNotEmpty($list[$cohort4->id]);
$this->assertRegExp('/[^\)]$/', $list[$cohort4->id]);
}
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']);
}
}
+47 -2
View File
@@ -36,6 +36,7 @@
class phpunit_data_generator {
protected $usercounter = 0;
protected $categorycount = 0;
protected $cohortcount = 0;
protected $coursecount = 0;
protected $scalecount = 0;
protected $groupcount = 0;
@@ -218,7 +219,7 @@ EOD;
* @param array $options
* @return stdClass course category record
*/
function create_category($record=null, array $options=null) {
public function create_category($record=null, array $options=null) {
global $DB, $CFG;
require_once("$CFG->dirroot/course/lib.php");
@@ -270,6 +271,50 @@ EOD;
return $DB->get_record('course_categories', array('id'=>$catid), '*', MUST_EXIST);
}
/**
* Create test cohort.
* @param array|stdClass $record
* @param array $options
* @return stdClass cohort record
*/
public function create_cohort($record=null, array $options=null) {
global $DB, $CFG;
require_once("$CFG->dirroot/cohort/lib.php");
$this->cohortcount++;
$i = $this->cohortcount;
$record = (array)$record;
if (!isset($record['contextid'])) {
$record['contextid'] = context_system::instance()->id;
}
if (!isset($record['name'])) {
$record['name'] = 'Cohort '.$i;
}
if (!isset($record['idnumber'])) {
$record['idnumber'] = '';
}
if (!isset($record['description'])) {
$record['description'] = "Test cohort $i\n$this->loremipsum";
}
if (!isset($record['descriptionformat'])) {
$record['descriptionformat'] = FORMAT_MOODLE;
}
if (!isset($record['component'])) {
$record['component'] = '';
}
$id = cohort_add_cohort((object)$record);
return $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
}
/**
* Create a test course
* @param array|stdClass $record
@@ -277,7 +322,7 @@ EOD;
* 'createsections'=>bool precreate all sections
* @return stdClass course record
*/
function create_course($record=null, array $options=null) {
public function create_course($record=null, array $options=null) {
global $DB, $CFG;
require_once("$CFG->dirroot/course/lib.php");
+12
View File
@@ -53,6 +53,18 @@ class core_phpunit_generator_testcase extends advanced_testcase {
$this->assertRegExp('/^Test course category \d/', $category->description);
$this->assertSame(FORMAT_MOODLE, $category->descriptionformat);
$count = $DB->count_records('cohort');
$cohort = $generator->create_cohort();
$this->assertEquals($count+1, $DB->count_records('cohort'));
$this->assertEquals(context_system::instance()->id, $cohort->contextid);
$this->assertRegExp('/^Cohort \d/', $cohort->name);
$this->assertSame('', $cohort->idnumber);
$this->assertRegExp('/^Test cohort \d/', $cohort->description);
$this->assertSame(FORMAT_MOODLE, $cohort->descriptionformat);
$this->assertSame('', $cohort->component);
$this->assertLessThanOrEqual(time(), $cohort->timecreated);
$this->assertSame($cohort->timecreated, $cohort->timemodified);
$count = $DB->count_records('course');
$course = $generator->create_course();
$this->assertEquals($count+1, $DB->count_records('course'));
+3
View File
@@ -39,6 +39,9 @@
<testsuite name="core_files">
<directory suffix="_test.php">lib/filestorage/tests</directory>
</testsuite>
<testsuite name="core_cohort">
<directory suffix="_test.php">cohort/tests</directory>
</testsuite>
<testsuite name="core_grade">
<directory suffix="_test.php">lib/grade/tests</directory>
<directory suffix="_test.php">grade/tests</directory>