Skip to content

Commit

Permalink
MDL-61601 cohort: Add theme support for cohorts
Browse files Browse the repository at this point in the history
If enabled $CFG->allowcohortthemes, then themes can be set at the cohort level.
This will affect all users with only one cohort or more than one but with the same theme.
The default theme order will be: course, category, session, user, cohort, site.
  • Loading branch information
sarjona committed Apr 6, 2018
1 parent 2bd2660 commit 88cb8b7
Show file tree
Hide file tree
Showing 18 changed files with 478 additions and 16 deletions.
1 change: 1 addition & 0 deletions admin/settings/appearance.php
Expand Up @@ -22,6 +22,7 @@
$temp->add(new admin_setting_configcheckbox('allowuserthemes', new lang_string('allowuserthemes', 'admin'), new lang_string('configallowuserthemes', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('allowcoursethemes', new lang_string('allowcoursethemes', 'admin'), new lang_string('configallowcoursethemes', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('allowcategorythemes', new lang_string('allowcategorythemes', 'admin'), new lang_string('configallowcategorythemes', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('allowcohortthemes', new lang_string('allowcohortthemes', 'admin'), new lang_string('configallowcohortthemes', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('allowthemechangeonurl', new lang_string('allowthemechangeonurl', 'admin'), new lang_string('configallowthemechangeonurl', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('allowuserblockhiding', new lang_string('allowuserblockhiding', 'admin'), new lang_string('configallowuserblockhiding', 'admin'), 1));
$temp->add(new admin_setting_configcheckbox('allowblockstodock', new lang_string('allowblockstodock', 'admin'), new lang_string('configallowblockstodock', 'admin'), 1));
Expand Down
4 changes: 4 additions & 0 deletions cohort/classes/external/cohort_summary_exporter.php
Expand Up @@ -64,6 +64,10 @@ public static function define_properties() {
),
'visible' => array(
'type' => PARAM_BOOL,
),
'theme' => array(
'type' => PARAM_THEME,
'null' => NULL_ALLOWED
)
);
}
Expand Down
6 changes: 6 additions & 0 deletions cohort/edit_form.php
Expand Up @@ -32,6 +32,7 @@ class cohort_edit_form extends moodleform {
* Define the cohort edit form
*/
public function definition() {
global $CFG;

$mform = $this->_form;
$editoroptions = $this->_customdata['editoroptions'];
Expand All @@ -54,6 +55,11 @@ public function definition() {
$mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions);
$mform->setType('description_editor', PARAM_RAW);

if (!empty($CFG->allowcohortthemes)) {
$themes = array_merge(array('' => get_string('forceno')), cohort_get_list_of_themes());
$mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
}

$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);

Expand Down
45 changes: 44 additions & 1 deletion cohort/externallib.php
Expand Up @@ -54,6 +54,10 @@ public static function create_cohorts_parameters() {
'description' => new external_value(PARAM_RAW, 'cohort description', VALUE_OPTIONAL),
'descriptionformat' => new external_format_value('description', VALUE_DEFAULT),
'visible' => new external_value(PARAM_BOOL, 'cohort visible', VALUE_OPTIONAL, true),
'theme' => new external_value(PARAM_THEME,
'the cohort theme. The allowcohortthemes setting must be enabled on Moodle',
VALUE_OPTIONAL
),
)
)
)
Expand All @@ -74,6 +78,8 @@ public static function create_cohorts($cohorts) {

$params = self::validate_parameters(self::create_cohorts_parameters(), array('cohorts' => $cohorts));

$availablethemes = cohort_get_list_of_themes();

$transaction = $DB->start_delegated_transaction();

$syscontext = context_system::instance();
Expand Down Expand Up @@ -107,6 +113,15 @@ public static function create_cohorts($cohorts) {
self::validate_context($context);
require_capability('moodle/cohort:manage', $context);

// Make sure theme is valid.
if (isset($cohort->theme)) {
if (!empty($CFG->allowcohortthemes)) {
if (empty($availablethemes[$cohort->theme])) {
throw new moodle_exception('errorinvalidparam', 'webservice', '', 'theme');
}
}
}

// Validate format.
$cohort->descriptionformat = external_validate_format($cohort->descriptionformat);
$cohort->id = cohort_add_cohort($cohort);
Expand Down Expand Up @@ -137,6 +152,7 @@ public static function create_cohorts_returns() {
'description' => new external_value(PARAM_RAW, 'cohort description'),
'descriptionformat' => new external_format_value('description'),
'visible' => new external_value(PARAM_BOOL, 'cohort visible'),
'theme' => new external_value(PARAM_THEME, 'cohort theme', VALUE_OPTIONAL),
)
)
);
Expand Down Expand Up @@ -223,7 +239,7 @@ public static function get_cohorts_parameters() {
* @since Moodle 2.5
*/
public static function get_cohorts($cohortids = array()) {
global $DB;
global $DB, $CFG;

$params = self::validate_parameters(self::get_cohorts_parameters(), array('cohortids' => $cohortids));

Expand All @@ -245,6 +261,11 @@ public static function get_cohorts($cohortids = array()) {
throw new required_capability_exception($context, 'moodle/cohort:view', 'nopermissions', '');
}

// Only return theme when $CFG->allowcohortthemes is enabled.
if (!empty($cohort->theme) && empty($CFG->allowcohortthemes)) {
$cohort->theme = null;
}

list($cohort->description, $cohort->descriptionformat) =
external_format_text($cohort->description, $cohort->descriptionformat,
$context->id, 'cohort', 'description', $cohort->id);
Expand All @@ -271,6 +292,7 @@ public static function get_cohorts_returns() {
'description' => new external_value(PARAM_RAW, 'cohort description'),
'descriptionformat' => new external_format_value('description'),
'visible' => new external_value(PARAM_BOOL, 'cohort visible'),
'theme' => new external_value(PARAM_THEME, 'cohort theme', VALUE_OPTIONAL),
)
)
);
Expand Down Expand Up @@ -367,6 +389,12 @@ public static function search_cohorts($query, $context, $includes = 'parents', $
$cohorts = array();
foreach ($results as $key => $cohort) {
$cohortcontext = context::instance_by_id($cohort->contextid);

// Only return theme when $CFG->allowcohortthemes is enabled.
if (!empty($cohort->theme) && empty($CFG->allowcohortthemes)) {
$cohort->theme = null;
}

if (!isset($cohort->description)) {
$cohort->description = '';
}
Expand Down Expand Up @@ -399,6 +427,7 @@ public static function search_cohorts_returns() {
'description' => new external_value(PARAM_RAW, 'cohort description'),
'descriptionformat' => new external_format_value('description'),
'visible' => new external_value(PARAM_BOOL, 'cohort visible'),
'theme' => new external_value(PARAM_THEME, 'cohort theme', VALUE_OPTIONAL),
))
)
));
Expand Down Expand Up @@ -432,6 +461,10 @@ public static function update_cohorts_parameters() {
'description' => new external_value(PARAM_RAW, 'cohort description', VALUE_OPTIONAL),
'descriptionformat' => new external_format_value('description', VALUE_DEFAULT),
'visible' => new external_value(PARAM_BOOL, 'cohort visible', VALUE_OPTIONAL),
'theme' => new external_value(PARAM_THEME,
'the cohort theme. The allowcohortthemes setting must be enabled on Moodle',
VALUE_OPTIONAL
),
)
)
)
Expand All @@ -452,6 +485,8 @@ public static function update_cohorts($cohorts) {

$params = self::validate_parameters(self::update_cohorts_parameters(), array('cohorts' => $cohorts));

$availablethemes = cohort_get_list_of_themes();

$transaction = $DB->start_delegated_transaction();
$syscontext = context_system::instance();

Expand Down Expand Up @@ -490,6 +525,14 @@ public static function update_cohorts($cohorts) {
require_capability('moodle/cohort:manage', $context);
}

// Make sure theme is valid.
if (!empty($cohort->theme) && !empty($CFG->allowcohortthemes)) {
if (empty($availablethemes[$cohort->theme])) {
$debuginfo = 'The following cohort theme is not installed on this site: '.$cohort->theme;
throw new moodle_exception('errorinvalidparam', 'webservice', '', 'theme', $debuginfo);
}
}

if (!empty($cohort->description)) {
$cohort->descriptionformat = external_validate_format($cohort->descriptionformat);
}
Expand Down
71 changes: 69 additions & 2 deletions cohort/lib.php
Expand Up @@ -38,7 +38,7 @@
* @return int new cohort id
*/
function cohort_add_cohort($cohort) {
global $DB;
global $DB, $CFG;

if (!isset($cohort->name)) {
throw new coding_exception('Missing cohort name in cohort_add_cohort().');
Expand All @@ -58,6 +58,12 @@ function cohort_add_cohort($cohort) {
if (empty($cohort->component)) {
$cohort->component = '';
}
if (empty($CFG->allowcohortthemes) && isset($cohort->theme)) {
unset($cohort->theme);
}
if (empty($cohort->theme) || empty($CFG->allowcohortthemes)) {
$cohort->theme = '';
}
if (!isset($cohort->timecreated)) {
$cohort->timecreated = time();
}
Expand All @@ -83,11 +89,15 @@ function cohort_add_cohort($cohort) {
* @return void
*/
function cohort_update_cohort($cohort) {
global $DB;
global $DB, $CFG;
if (property_exists($cohort, 'component') and empty($cohort->component)) {
// prevent NULLs
$cohort->component = '';
}
// Only unset the cohort theme if allowcohortthemes is enabled to prevent the value from being overwritten.
if (empty($CFG->allowcohortthemes) && isset($cohort->theme)) {
unset($cohort->theme);
}
$cohort->timemodified = time();
$DB->update_record('cohort', $cohort);

Expand Down Expand Up @@ -478,6 +488,47 @@ function cohort_get_all_cohorts($page = 0, $perpage = 25, $search = '') {
return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts, 'allcohorts' => $allcohorts);
}

/**
* Get all the cohorts where the given user is member of.
*
* @param int $userid
* @return array Array
*/
function cohort_get_user_cohorts($userid) {
global $DB;

$sql = 'SELECT c.*
FROM {cohort} c
JOIN {cohort_members} cm ON c.id = cm.cohortid
WHERE cm.userid = ? AND c.visible = 1';
return $DB->get_records_sql($sql, array($userid));
}

/**
* Get the user cohort theme.
*
* If the user is member of one cohort, will return this cohort theme (if defined).
* If the user is member of 2 or more cohorts, will return the theme if all them have the same
* theme (null themes are ignored).
*
* @param int $userid
* @return string|null
*/
function cohort_get_user_cohort_theme($userid) {
$cohorts = cohort_get_user_cohorts($userid);
$theme = null;
foreach ($cohorts as $cohort) {
if (!empty($cohort->theme)) {
if (null === $theme) {
$theme = $cohort->theme;
} else if ($theme != $cohort->theme) {
return null;
}
}
}
return $theme;
}

/**
* Returns list of contexts where cohorts are present but current user does not have capability to view/manage them.
*
Expand Down Expand Up @@ -568,3 +619,19 @@ function core_cohort_inplace_editable($itemtype, $itemid, $newvalue) {
return \core_cohort\output\cohortidnumber::update($itemid, $newvalue);
}
}

/**
* Returns a list of valid themes which can be displayed in a selector.
*
* @return array as (string)themename => (string)get_string_theme
*/
function cohort_get_list_of_themes() {
$themes = array();
$allthemes = get_list_of_themes();
foreach ($allthemes as $key => $theme) {
if (empty($theme->hidefromselector)) {
$themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
}
}
return $themes;
}
40 changes: 36 additions & 4 deletions cohort/tests/behat/upload_cohorts.feature
Expand Up @@ -50,7 +50,7 @@ Feature: A privileged user can create cohorts using a CSV file
And the "class" attribute of "cohort name 5" "table_row" should contain "dimmed_text"
And ".dimmed_text" "css_element" should not exist in the "cohort name 6" "table_row"

@javascript
@javascript @_file_upload
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"
Expand Down Expand Up @@ -81,7 +81,7 @@ Feature: A privileged user can create cohorts using a CSV file
| Cat 2 | cohort name 5 | cohortid5 | | 0 | Created manually |
| Cat 3 | cohort name 6 | cohortid6 | | 0 | Created manually |

@javascript
@javascript @_file_upload
Scenario: Upload cohorts with default category context as manager
Given the following "users" exist:
| username | firstname | lastname | email |
Expand All @@ -107,7 +107,7 @@ Feature: A privileged user can create cohorts using a CSV file
And I press "Upload cohorts"
And I should see "Uploaded 6 cohorts"

@javascript
@javascript @_file_upload
Scenario: Upload cohorts with conflicting id number
Given the following "cohorts" exist:
| name | idnumber |
Expand All @@ -128,7 +128,7 @@ Feature: A privileged user can create cohorts using a CSV file
| cohort name 6 | cohortid6 | | Cat 3 | |
And "Upload cohorts" "button" should not exist

@javascript
@javascript @_file_upload
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"
Expand Down Expand Up @@ -161,3 +161,35 @@ Feature: A privileged user can create cohorts using a CSV file
And I should not see "not found or you"
And I press "Upload cohorts"
And I should see "Uploaded 5 cohorts"

@javascript @_file_upload
Scenario: Upload cohorts with theme
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/uploadcohorts4.csv" file to "File" filemanager
And I click on "Preview" "button"
Then the following should exist in the "previewuploadedcohorts" table:
| name | idnumber | description | Context | visible | theme | Status |
| cohort name 1 | cohortid1 | first description | System | 1 | boost | |
| cohort name 2 | cohortid2 | | System | 1 | | |
| cohort name 3 | cohortid3 | | Miscellaneous | 0 | boost | |
| cohort name 4 | cohortid4 | | Cat 1 | 1 | clean | |
| cohort name 5 | cohortid5 | | Cat 2 | 0 | | |
| cohort name 6 | cohortid6 | | Cat 3 | 1 | clean | |
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 |

0 comments on commit 88cb8b7

Please sign in to comment.