Project

General

Profile

Bug #5625 » group_category.php

Yoselyn Castillo, 07/03/2013 20:06

 
1
<?php
2
/* For licensing terms, see /license.txt */
3

    
4
/**
5
 *	@package chamilo.group
6
 */
7

    
8
// Name of the language file that needs to be included
9
$language_file = 'group';
10

    
11
require_once '../inc/global.inc.php';
12
$this_section = SECTION_COURSES;
13
$current_course_tool  = TOOL_GROUP;
14

    
15
// Notice for unauthorized people.
16
api_protect_course_script(true);
17

    
18
if (!api_is_allowed_to_edit(false,true) || !(isset ($_GET['id']) || isset ($_POST['id']) || isset ($_GET['action']) || isset ($_POST['action']))) {
19
	api_not_allowed();
20
}
21

    
22
/**
23
 * Function to check the given max number of members per group
24
 */
25
function check_max_number_of_members($value) {
26
	$max_member_no_limit = $value['max_member_no_limit'];
27
	if ($max_member_no_limit == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
28
		return true;
29
	}
30
	$max_member = $value['max_member'];
31
	return is_numeric($max_member);
32
}
33

    
34
/**
35
 * Function to check the number of groups per user
36
 */
37
function check_groups_per_user($value) {
38
	$groups_per_user = $value['groups_per_user'];
39
	if (isset ($_POST['id']) && intval($groups_per_user) != GroupManager::GROUP_PER_MEMBER_NO_LIMIT && GroupManager::get_current_max_groups_per_user($_POST['id']) > intval($groups_per_user)) {
40
		return false;
41
	}
42
	return true;
43
}
44

    
45
if (api_get_setting('allow_group_categories') == 'true') {
46
	if (isset ($_GET['id'])) {
47
		$category = GroupManager :: get_category($_GET['id']);
48
		$nameTools = get_lang('EditGroupCategory').': '.$category['title'];
49
	} else {
50
		$nameTools = get_lang('AddCategory');
51
		// Default values for new category
52
		$category = array(
53
            'groups_per_user' => 1, 
54
            'doc_state' => GroupManager::TOOL_PRIVATE, 
55
            'work_state' => GroupManager::TOOL_PRIVATE, 
56
            'wiki_state' => GroupManager::TOOL_PRIVATE , 
57
            'chat_state' => GroupManager::TOOL_PRIVATE, 
58
            'calendar_state' => GroupManager::TOOL_PRIVATE, 
59
            'announcements_state'=> GroupManager::TOOL_PRIVATE, 
60
            'forum_state' => GroupManager::TOOL_PRIVATE, 
61
            'max_student' => 0);
62
	}
63
} else {
64
	$category = GroupManager :: get_category($_GET['id']);
65
	$nameTools = get_lang('PropModify');
66
}
67

    
68
$htmlHeadXtra[] = '<script>
69
$(document).ready( function() {
70
    $("#max_member").on("focus", function() {
71
        $("#max_member_selected").attr("checked", true);
72
    });
73
});
74
 </script>';
75
 
76
$interbreadcrumb[] = array ('url' => 'group.php', 'name' => get_lang('Groups'));
77

    
78
$course_id = api_get_course_int_id();
79

    
80
// Build the form
81
if (isset ($_GET['id'])) {
82
	// Update settings of existing category
83
	$action = 'update_settings';
84
	$form = new FormValidator('group_category', 'post', '?id='.$category['id']);
85
	$form->addElement('header', $nameTools);
86
	$form->addElement('hidden', 'id');
87
} else {
88
    // Checks if the field was created in the table Category. It creates it if is neccesary
89
    $table_category = Database :: get_course_table(TABLE_GROUP_CATEGORY);
90
	if (!Database::query("SELECT wiki_state FROM $table_category WHERE c_id = $course_id")) {
91
    	Database::query("ALTER TABLE $table_category ADD wiki_state tinyint(3) UNSIGNED NOT NULL default '1' WHERE c_id = $course_id");
92
    }
93
	// Create a new category
94
	$action = 'add_category';
95
	$form = new FormValidator('group_category');
96
}
97

    
98
$form->addElement('html', '<div class="sectiontitle" >'.$nameTools);
99
$form->addElement('html', '</div>');
100

    
101
// If categories allowed, show title & description field
102
if (api_get_setting('allow_group_categories') == 'true') {
103
	$form->add_textfield('title', get_lang('Title'));
104
	$form->addElement('textarea', 'description', get_lang('Description'), array('cols' => 50, 'rows' => 6));
105
} else {
106
	$form->addElement('hidden', 'title');
107
	$form->addElement('hidden', 'description');
108
}
109

    
110
// Action
111
$form->addElement('hidden', 'action');
112

    
113
// Groups per user
114
$group = array ();
115
$group[] = $form->createElement('static', null, null, get_lang('QtyOfUserCanSubscribe_PartBeforeNumber'));
116
$possible_values = array ();
117
for ($i = 1; $i <= 10; $i ++) {
118
	$possible_values[$i] = $i;
119
}
120
$possible_values[GroupManager::GROUP_PER_MEMBER_NO_LIMIT] = get_lang('All');
121
$group[] = $form->createElement('select', 'groups_per_user', null, $possible_values);
122
$group[] = $form->createElement('static', null, null, get_lang('QtyOfUserCanSubscribe_PartAfterNumber'));
123
$form->addGroup($group, 'limit_group', get_lang('GroupLimit'), ' ', false);
124
$form->addRule('limit_group', get_lang('MaxGroupsPerUserInvalid'), 'callback', 'check_groups_per_user');
125
// Default settings for new groups
126
//$form->addElement('static', null, '<b>'.get_lang('DefaultSettingsForNewGroups').'</b>');
127

    
128
$form->addElement('html', '<br /><br /><div class="sectiontitle" >'.get_lang('DefaultSettingsForNewGroups'));
129
$form->addElement('html', '</div>');
130

    
131
// Members per group
132
$form->addElement('radio', 'max_member_no_limit', get_lang('GroupLimit'), get_lang('NoLimit'), GroupManager::MEMBER_PER_GROUP_NO_LIMIT);
133
$group = array ();
134
$group[] = $form->createElement('radio', 'max_member_no_limit', null, get_lang('MaximumOfParticipants'), 1, array('id' => 'max_member_selected'));
135
$group[] = $form->createElement('text', 'max_member', null, array ('size' => 2, 'id' => 'max_member'));
136
$group[] = $form->createElement('static', null, null, get_lang('GroupPlacesThis'));
137
$form->addGroup($group, 'max_member_group', null, '', false);
138
$form->addRule('max_member_group', get_lang('InvalidMaxNumberOfMembers'), 'callback', 'check_max_number_of_members');
139

    
140
// Self registration
141
$form->addElement('checkbox', 'self_reg_allowed', get_lang('GroupSelfRegistration'), get_lang('GroupAllowStudentRegistration'), 1);
142
$form->addElement('checkbox', 'self_unreg_allowed', null, get_lang('GroupAllowStudentUnregistration'), 1);
143

    
144
// Documents settings
145
$form->addElement('radio', 'doc_state', get_lang('GroupDocument'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
146
$form->addElement('radio', 'doc_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
147
$form->addElement('radio', 'doc_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
148

    
149
// Work settings
150
$form->addElement('radio', 'work_state', get_lang('GroupWork'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
151
$form->addElement('radio', 'work_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
152
$form->addElement('radio', 'work_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
153

    
154
// Calendar settings
155
$form->addElement('radio', 'calendar_state', get_lang('GroupCalendar'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
156
$form->addElement('radio', 'calendar_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
157
$form->addElement('radio', 'calendar_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
158

    
159
// Announcements settings
160
$form->addElement('radio', 'announcements_state', get_lang('GroupAnnouncements'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
161
$form->addElement('radio', 'announcements_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
162
$form->addElement('radio', 'announcements_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
163

    
164
// Forum settings
165
$form->addElement('radio', 'forum_state', get_lang('GroupForum'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
166
$form->addElement('radio', 'forum_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
167
$form->addElement('radio', 'forum_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
168

    
169
// Wiki Settings
170
$form->addElement('radio', 'wiki_state', get_lang('GroupWiki'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
171
$form->addElement('radio', 'wiki_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
172
$form->addElement('radio', 'wiki_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
173

    
174
// Chat Settings
175
$form->addElement('radio', 'chat_state', get_lang('Chat'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
176
$form->addElement('radio', 'chat_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
177
$form->addElement('radio', 'chat_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
178

    
179
// Submit
180
$form->addElement('style_submit_button', 'submit', get_lang('PropModify'), 'class="save"');
181

    
182
// If form validates -> save data
183
if ($form->validate()) {
184
	$values = $form->exportValues();
185
	if ($values['max_member_no_limit'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
186
		$max_member = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
187
	} else {
188
		$max_member = $values['max_member'];
189
	}
190
	$self_reg_allowed = isset($values['self_reg_allowed']) ? $values['self_reg_allowed'] : 0;
191
	$self_unreg_allowed = isset($values['self_unreg_allowed']) ? $values['self_unreg_allowed'] : 0;
192
	switch ($values['action']) {
193
		case 'update_settings':
194
			GroupManager :: update_category($values['id'], $values['title'], $values['description'], $values['doc_state'], $values['work_state'], $values['calendar_state'], $values['announcements_state'], $values['forum_state'], $values['wiki_state'], $values['chat_state'], $self_reg_allowed, $self_unreg_allowed, $max_member, $values['groups_per_user']);
195
			$msg = urlencode(get_lang('GroupPropertiesModified'));
196
			header('Location: group.php?action=show_msg&msg='.$msg.'&category='.$values['id']);
197
			break;
198
		case 'add_category':
199
			GroupManager :: create_category($values['title'], $values['description'], $values['doc_state'], $values['work_state'], $values['calendar_state'], $values['announcements_state'], $values['forum_state'], $values['wiki_state'], $values['chat_state'], $self_reg_allowed, $self_unreg_allowed, $max_member, $values['groups_per_user']);
200
			$msg = urlencode(get_lang('CategoryCreated'));
201
			header('Location: group.php?action=show_msg&msg='.$msg);
202
			break;
203
	}
204
}
205

    
206
// Else display the form
207
Display :: display_header($nameTools, 'Group');
208

    
209
// actions bar
210
echo '<div class="actions">';
211
echo '<a href="group.php">'.Display::return_icon('back.png', get_lang('BackToGroupList'),'',ICON_SIZE_MEDIUM).'</a>';
212
echo '</div>';
213

    
214
$defaults = $category;
215
$defaults['action'] = $action;
216
if ($defaults['max_student'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
217
	$defaults['max_member_no_limit'] = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
218
} else {
219
	$defaults['max_member_no_limit'] = 1;
220
	$defaults['max_member'] = $defaults['max_student'];
221
}
222
$form->setDefaults($defaults);
223
$form->display();
224

    
225
Display :: display_footer();
(6-6/6)