Skip to content

Commit

Permalink
MDL-35468 cohorts: upload cohorts from csv file
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Sep 12, 2014
1 parent bd855fd commit 5076b96
Show file tree
Hide file tree
Showing 10 changed files with 864 additions and 4 deletions.
7 changes: 3 additions & 4 deletions cohort/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
require('../config.php');
require($CFG->dirroot.'/cohort/lib.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/coursecatlib.php');

$contextid = optional_param('contextid', 0, PARAM_INT);
$page = optional_param('page', 0, PARAM_INT);
Expand Down Expand Up @@ -124,10 +123,10 @@
$cohortcontext = context::instance_by_id($cohort->contextid);
if ($showall) {
if ($cohortcontext->contextlevel == CONTEXT_COURSECAT) {
$cat = coursecat::get($cohortcontext->instanceid);
$line[] = html_writer::link(new moodle_url('/cohort/index.php' , array('contextid' => $cohort->contextid)), $cat->get_formatted_name());
$line[] = html_writer::link(new moodle_url('/cohort/index.php' ,
array('contextid' => $cohort->contextid)), $cohortcontext->get_context_name(false));
} else {
$line[] = get_string('coresystem');
$line[] = $cohortcontext->get_context_name(false);
}
}
$line[] = format_string($cohort->name);
Expand Down
6 changes: 6 additions & 0 deletions cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,12 @@ function cohort_edit_controls(context $context, moodle_url $currenturl) {
if ($currenturl->get_path() === $addurl->get_path() && !$currenturl->param('id')) {
$currenttab = 'addcohort';
}

$uploadurl = new moodle_url('/cohort/upload.php', array('contextid' => $context->id));
$tabs[] = new tabobject('uploadcohorts', $uploadurl, get_string('uploadcohorts', 'cohort'));
if ($currenturl->get_path() === $uploadurl->get_path()) {
$currenttab = 'uploadcohorts';
}
}
if (count($tabs) > 1) {
return new tabtree($tabs, $currenttab);
Expand Down
156 changes: 156 additions & 0 deletions cohort/tests/behat/upload_cohorts.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
@core @core_cohort @_file_upload
Feature: A privileged user can create cohorts using a CSV file
In order to create cohorts using a CSV file
As an admin
I need to be able to upload a CSV file and navigate through the upload process

Background:
Given the following "categories" exist:
| name | category | idnumber |
| Cat 1 | 0 | CAT1 |
| Cat 2 | 0 | CAT2 |
| Cat 3 | CAT1 | CAT3 |

@javascript
Scenario: Upload cohorts with default System context as admin
When I log in as "admin"
And I navigate to "Cohorts" node in "Site administration > Users > Accounts"
And I follow "Upload cohorts"
And I upload "cohort/tests/fixtures/uploadcohorts1.csv" file to "File" filemanager
And I click on "Preview" "button"
Then the following should exist in the "previewuploadedcohorts" table:
| name | idnumber | description | Context | Status |
| cohort name 1 | cohortid1 | first description | System | |
| cohort name 2 | cohortid2 | | System | |
| cohort name 3 | cohortid3 | | Miscellaneous | |
| cohort name 4 | cohortid4 | | Cat 1 | |
| cohort name 5 | cohortid5 | | Cat 2 | |
| cohort name 6 | cohortid6 | | Cat 3 | |
And I press "Upload cohorts"
And I should see "Uploaded 6 cohorts"
And I press "Continue"
And the following should exist in the "cohorts" table:
| Name | Cohort ID | Description | Cohort size | Source |
| cohort name 1 | cohortid1 | first description | 0 | Created manually |
| cohort name 2 | cohortid2 | | 0 | Created manually |
And I follow "All cohorts"
And the following should exist in the "cohorts" table:
| Category | Name | Cohort ID | Description | Cohort size | Source |
| System | cohort name 1 | cohortid1 | first description | 0 | Created manually |
| System | cohort name 2 | cohortid2 | | 0 | Created manually |
| Miscellaneous | cohort name 3 | cohortid3 | | 0 | Created manually |
| Cat 1 | cohort name 4 | cohortid4 | | 0 | Created manually |
| Cat 2 | cohort name 5 | cohortid5 | | 0 | Created manually |
| Cat 3 | cohort name 6 | cohortid6 | | 0 | Created manually |

@javascript
Scenario: Upload cohorts with default category context as admin
When I log in as "admin"
And I navigate to "Cohorts" node in "Site administration > Users > Accounts"
And I follow "Upload cohorts"
And I upload "cohort/tests/fixtures/uploadcohorts1.csv" file to "File" filemanager
And I set the field "Default context" to "Cat 1 / Cat 3"
And I click on "Preview" "button"
Then the following should exist in the "previewuploadedcohorts" table:
| name | idnumber | description | Context | Status |
| cohort name 1 | cohortid1 | first description | Cat 3 | |
| cohort name 2 | cohortid2 | | Cat 3 | |
| cohort name 3 | cohortid3 | | Miscellaneous | |
| cohort name 4 | cohortid4 | | Cat 1 | |
| cohort name 5 | cohortid5 | | Cat 2 | |
| cohort name 6 | cohortid6 | | Cat 3 | |
And I press "Upload cohorts"
And I should see "Uploaded 6 cohorts"
And I press "Continue"
And I should see "Category: Cat 3: available cohorts (3)"
And I navigate to "Cohorts" node in "Site administration > Users > Accounts"
And I follow "All cohorts"
And the following should exist in the "cohorts" table:
| Category | Name | Cohort ID | Description | Cohort size | Source |
| Cat 3 | cohort name 1 | cohortid1 | first description | 0 | Created manually |
| Cat 3 | cohort name 2 | cohortid2 | | 0 | Created manually |
| Miscellaneous | cohort name 3 | cohortid3 | | 0 | Created manually |
| Cat 1 | cohort name 4 | cohortid4 | | 0 | Created manually |
| Cat 2 | cohort name 5 | cohortid5 | | 0 | Created manually |
| Cat 3 | cohort name 6 | cohortid6 | | 0 | Created manually |

@javascript
Scenario: Upload cohorts with default category context as manager
Given the following "users" exist:
| username | firstname | lastname | email |
| user1 | User | 1 | user1@moodlemoodle.com |
And the following "role assigns" exist:
| user | role | contextlevel | reference |
| user1 | manager | Category | CAT1 |
When I log in as "user1"
And I follow "Courses"
And I follow "Cat 1"
And I navigate to "Cohorts" node in "Category: Cat 1"
And I follow "Upload cohorts"
And I upload "cohort/tests/fixtures/uploadcohorts1.csv" file to "File" filemanager
And I click on "Preview" "button"
Then the following should exist in the "previewuploadedcohorts" table:
| name | idnumber | description | Context | Status |
| cohort name 1 | cohortid1 | first description | Cat 1 | |
| cohort name 2 | cohortid2 | | Cat 1 | |
| cohort name 3 | cohortid3 | | Cat 1 | Category Miscellaneous not found or you don't have permission to create a cohort there. The default context will be used. |
| cohort name 4 | cohortid4 | | Cat 1 | |
| cohort name 5 | cohortid5 | | Cat 1 | Category CAT2 not found or you don't have permission to create a cohort there. The default context will be used. |
| cohort name 6 | cohortid6 | | Cat 3 | |
And I press "Upload cohorts"
And I should see "Uploaded 6 cohorts"

@javascript
Scenario: Upload cohorts with conflicting id number
Given the following "cohorts" exist:
| name | idnumber |
| Cohort | cohortid2 |
When I log in as "admin"
And I navigate to "Cohorts" node in "Site administration > Users > Accounts"
And I follow "Upload cohorts"
And I upload "cohort/tests/fixtures/uploadcohorts1.csv" file to "File" filemanager
And I click on "Preview" "button"
Then I should see "Errors were found in CSV data. See details below."
Then the following should exist in the "previewuploadedcohorts" table:
| name | idnumber | description | Context | Status |
| cohort name 1 | cohortid1 | first description | System | |
| cohort name 2 | cohortid2 | | System | Cohort with the same ID number already exists |
| cohort name 3 | cohortid3 | | Miscellaneous | |
| cohort name 4 | cohortid4 | | Cat 1 | |
| cohort name 5 | cohortid5 | | Cat 2 | |
| cohort name 6 | cohortid6 | | Cat 3 | |
And "Upload cohorts" "button" should not exist

@javascript
Scenario: Upload cohorts with different ways of specifying context
When I log in as "admin"
And I navigate to "Cohorts" node in "Site administration > Users > Accounts"
And I follow "Upload cohorts"
And I upload "cohort/tests/fixtures/uploadcohorts2.csv" file to "File" filemanager
And I click on "Preview" "button"
Then the following should exist in the "previewuploadedcohorts" table:
| name | idnumber | description | Context | Status |
| Specify category as name | cohortid1 | | Miscellaneous | |
| Specify category as idnumber | cohortid2 | | Cat 1 | |
| Specify category as id | cohortid3 | | Miscellaneous | |
| Specify category as path | cohortid4 | | Cat 3 | |
| Specify category_id | cohortid5 | | Miscellaneous | |
| Specify category_idnumber | cohortid6 | | Cat 1 | |
| Specify category_path | cohortid7 | | Cat 3 | |
And I should not see "not found or you"
And I press "Upload cohorts"
And I should see "Uploaded 7 cohorts"
And I press "Continue"
And I follow "Upload cohorts"
And I upload "cohort/tests/fixtures/uploadcohorts3.csv" file to "File" filemanager
And I click on "Preview" "button"
And the following should exist in the "previewuploadedcohorts" table:
| name | idnumber | description | Context | Status |
| Specify context as id (system) | cohortid8 | | System | |
| Specify context as name (system) | cohortid9 | | System | |
| Specify context as category name only | cohortid10 | | Cat 1 | |
| Specify context as category path | cohortid12 | | Cat 3 | |
| Specify context as category idnumber | cohortid13 | | Cat 2 | |
And I should not see "not found or you"
And I press "Upload cohorts"
And I should see "Uploaded 5 cohorts"
7 changes: 7 additions & 0 deletions cohort/tests/fixtures/uploadcohorts1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name,idnumber,description,category
cohort name 1,cohortid1,first description,
cohort name 2,cohortid2,,
cohort name 3,cohortid3,,Miscellaneous
cohort name 4,cohortid4,,CAT1
cohort name 5,cohortid5,,CAT2
cohort name 6,cohortid6,,CAT3
8 changes: 8 additions & 0 deletions cohort/tests/fixtures/uploadcohorts2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name,idnumber,description,category,category_id,category_idnumber,category_path
Specify category as name,cohortid1,,Miscellaneous,,,
Specify category as idnumber,cohortid2,,CAT1,,,
Specify category as id,cohortid3,,1,,,
Specify category as path,cohortid4,,Cat 1 / Cat 3,,,
Specify category_id,cohortid5,,,1,,
Specify category_idnumber,cohortid6,,,,CAT1,
Specify category_path,cohortid7,,,,,Cat 1 / Cat 3
6 changes: 6 additions & 0 deletions cohort/tests/fixtures/uploadcohorts3.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name,idnumber,description,context
Specify context as id (system),cohortid8,,1
Specify context as name (system),cohortid9,,System
Specify context as category name only,cohortid10,,Cat 1
Specify context as category path,cohortid12,,Cat 1 / Cat 3
Specify context as category idnumber,cohortid13,,CAT2
13 changes: 13 additions & 0 deletions cohort/tests/fixtures/uploadcohorts_test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name,idnumber,description,category
cohort name 1,cid1,first description,
cohort name 2,cid2,,
cohort name 3,cid3,,Miscellaneous
cohort name 4,cid4,,CAT1
cohort name 5,cid5,,CAT2
cohort name 6,cid6,,CAT3
cohort name 7,cid7,,CAT1
cohort name 8,cid8,,CAT2
cohort name 9,cid9,,CAT3
cohort name 10,cid10,,CAT1
cohort name 11,cid11,,CAT2
cohort name 12,cid1,,CAT3
93 changes: 93 additions & 0 deletions cohort/upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?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/>.

/**
* A form for cohort upload.
*
* @package core_cohort
* @copyright 2014 Marina Glancy
* @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_once($CFG->dirroot.'/cohort/upload_form.php');
require_once($CFG->libdir . '/csvlib.class.php');

$contextid = optional_param('contextid', 0, PARAM_INT);
$returnurl = optional_param('returnurl', '', PARAM_URL);

require_login();

if ($contextid) {
$context = context::instance_by_id($contextid, MUST_EXIST);
} else {
$context = context_system::instance();
}
if ($context->contextlevel != CONTEXT_COURSECAT && $context->contextlevel != CONTEXT_SYSTEM) {
print_error('invalidcontext');
}

require_capability('moodle/cohort:manage', $context);

$PAGE->set_context($context);
$baseurl = new moodle_url('/cohort/upload.php', array('contextid' => $context->id));
$PAGE->set_url($baseurl);
$PAGE->set_heading($COURSE->fullname);
$PAGE->set_pagelayout('admin');

if ($context->contextlevel == CONTEXT_COURSECAT) {
$PAGE->set_category_by_id($context->instanceid);
navigation_node::override_active_url(new moodle_url('/cohort/index.php', array('contextid' => $context->id)));
} else {
navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
}

$uploadform = new cohort_upload_form(null, array('contextid' => $context->id, 'returnurl' => $returnurl));

if ($returnurl) {
$returnurl = new moodle_url($returnurl);
} else {
$returnurl = new moodle_url('/cohort/index.php', array('contextid' => $context->id));
}

if ($uploadform->is_cancelled()) {
redirect($returnurl);
}

$strheading = get_string('uploadcohorts', 'cohort');
$PAGE->navbar->add($strheading);

echo $OUTPUT->header();
echo $OUTPUT->heading_with_help($strheading, 'uploadcohorts', 'cohort');

if ($editcontrols = cohort_edit_controls($context, $baseurl)) {
echo $OUTPUT->render($editcontrols);
}

if ($data = $uploadform->get_data()) {
$cohortsdata = $uploadform->get_cohorts_data();
foreach ($cohortsdata as $cohort) {
cohort_add_cohort($cohort);
}
echo $OUTPUT->notification(get_string('uploadedcohorts', 'cohort', count($cohortsdata)), 'notifysuccess');
echo $OUTPUT->continue_button($returnurl);
} else {
$uploadform->display();
}

echo $OUTPUT->footer();

Loading

0 comments on commit 5076b96

Please sign in to comment.