5bd8b31d45fdd35e5aed700b8010292987c1a59a
[moodle.git] / user / profile / index.php
1 <?php
3 require('../../config.php');
4 require_once($CFG->libdir.'/adminlib.php');
5 require_once($CFG->dirroot.'/user/profile/lib.php');
6 require_once($CFG->dirroot.'/user/profile/definelib.php');
8 admin_externalpage_setup('profilefields');
10 $action   = optional_param('action', '', PARAM_ALPHA);
12 $redirect = $CFG->wwwroot.'/user/profile/index.php';
14 $strchangessaved    = get_string('changessaved');
15 $strcancelled       = get_string('cancelled');
16 $strdefaultcategory = get_string('profiledefaultcategory', 'admin');
17 $strnofields        = get_string('profilenofieldsdefined', 'admin');
18 $strcreatefield     = get_string('profilecreatefield', 'admin');
21 /// Do we have any actions to perform before printing the header
23 switch ($action) {
24     case 'movecategory':
25         $id  = required_param('id', PARAM_INT);
26         $dir = required_param('dir', PARAM_ALPHA);
28         if (confirm_sesskey()) {
29             profile_move_category($id, $dir);
30         }
31         redirect($redirect);
32         break;
33     case 'movefield':
34         $id  = required_param('id', PARAM_INT);
35         $dir = required_param('dir', PARAM_ALPHA);
37         if (confirm_sesskey()) {
38             profile_move_field($id, $dir);
39         }
40         redirect($redirect);
41         break;
42     case 'deletecategory':
43         $id      = required_param('id', PARAM_INT);
44         $confirm = optional_param('confirm', 0, PARAM_BOOL);
46         if (data_submitted() and $confirm and confirm_sesskey()) {
47             profile_delete_category($id);
48             redirect($redirect);
49         }
51         //ask for confirmation
52         $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$id));
53         $optionsyes = array ('id'=>$id, 'confirm'=>1, 'action'=>'deletecategory', 'sesskey'=>sesskey());
54         echo $OUTPUT->header();
55         echo $OUTPUT->heading(get_string('profiledeletecategory', 'admin'));
57         $formcontinue = new single_button(new moodle_url($redirect, $optionsyes), get_string('yes'), 'post');
58         $formcancel = new single_button($redirect, get_string('no'), 'get');
59         echo $OUTPUT->confirm(get_string('profileconfirmcategorydeletion', 'admin', $fieldcount), $formcontinue, $formcancel);
60         echo $OUTPUT->footer();
61         die;
62         break;
63     case 'deletefield':
64         $id      = required_param('id', PARAM_INT);
65         $confirm = optional_param('confirm', 0, PARAM_BOOL);
67         if (data_submitted() and $confirm and confirm_sesskey()) {
68             profile_delete_field($id);
69             redirect($redirect);
70         }
72         //ask for confirmation
73         $datacount = $DB->count_records('user_info_data', array('fieldid'=>$id));
74         $optionsyes = array ('id'=>$id, 'confirm'=>1, 'action'=>'deletefield', 'sesskey'=>sesskey());
75         $strheading = get_string('profiledeletefield', 'admin');
76         $PAGE->navbar->add($strheading);
77         echo $OUTPUT->header();
78         echo $OUTPUT->heading($strheading);
79         $formcontinue = new single_button(new moodle_url($redirect, $optionsyes), get_string('yes'), 'post');
80         $formcancel = new single_button(new moodle_url($redirect), get_string('no'), 'get');
81         echo $OUTPUT->confirm(get_string('profileconfirmfielddeletion', 'admin', $datacount), $formcontinue, $formcancel);
82         echo $OUTPUT->footer();
83         die;
84         break;
85     case 'editfield':
86         $id       = optional_param('id', 0, PARAM_INT);
87         $datatype = optional_param('datatype', '', PARAM_ALPHA);
89         profile_edit_field($id, $datatype, $redirect);
90         die;
91         break;
92     case 'editcategory':
93         $id = optional_param('id', 0, PARAM_INT);
95         profile_edit_category($id, $redirect);
96         die;
97         break;
98     default:
99         //normal form
102 /// Print the header
103 echo $OUTPUT->header();
104 echo $OUTPUT->heading(get_string('profilefields', 'admin'));
106 /// Check that we have at least one category defined
107 if ($DB->count_records('user_info_category') == 0) {
108     $defaultcategory = new object();
109     $defaultcategory->name = $strdefaultcategory;
110     $defaultcategory->sortorder = 1;
111     $DB->insert_record('user_info_category', $defaultcategory);
112     redirect($redirect);
115 /// Show all categories
116 $categories = $DB->get_records('user_info_category', null, 'sortorder ASC');
118 foreach ($categories as $category) {
119     $table = new html_table();
120     $table->head  = array(get_string('profilefield', 'admin'), get_string('edit'));
121     $table->align = array('left', 'right');
122     $table->width = '95%';
123     $table->attributes['class'] = 'generaltable profilefield';
124     $table->data = array();
126     if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
127         foreach ($fields as $field) {
128             $table->data[] = array(format_string($field->name), profile_field_icons($field));
129         }
130     }
132     echo $OUTPUT->heading(format_string($category->name) .' '.profile_category_icons($category));
133     if (count($table->data)) {
134         echo html_writer::table($table);
135     } else {
136         echo $OUTPUT->notification($strnofields);
137     }
139 } /// End of $categories foreach
144 echo '<hr />';
145 echo '<div class="profileeditor">';
147 /// Create a new field link
148 $options = profile_list_datatypes();
149 $popupurl = new moodle_url('/user/profile/index.php?id=0&action=editfield');
150 echo $OUTPUT->single_select($popupurl, 'datatype', $options, '', array(''=>$strcreatefield), 'newfieldform');
152 //add a div with a class so themers can hide, style or reposition the text
153 html_writer::start_tag('div',array('class'=>'adminuseractionhint'));
154 echo get_string('or', 'lesson');
155 html_writer::end_tag('div');
157 /// Create a new category link
158 $options = array('action'=>'editcategory');
159 echo $OUTPUT->single_button(new moodle_url('index.php', $options), get_string('profilecreatecategory', 'admin'));
161 echo '</div>';
163 echo $OUTPUT->footer();
164 die;
167 /***** Some functions relevant to this script *****/
169 /**
170  * Create a string containing the editing icons for the user profile categories
171  * @param   object   the category object
172  * @return  string   the icon string
173  */
174 function profile_category_icons ($category) {
175     global $CFG, $USER, $DB, $OUTPUT;
177     $strdelete   = get_string('delete');
178     $strmoveup   = get_string('moveup');
179     $strmovedown = get_string('movedown');
180     $stredit     = get_string('edit');
182     $categorycount = $DB->count_records('user_info_category');
183     $fieldcount    = $DB->count_records('user_info_field', array('categoryid'=>$category->id));
185     /// Edit
186     $editstr = '<a title="'.$stredit.'" href="index.php?id='.$category->id.'&amp;action=editcategory"><img src="'.$OUTPUT->pix_url('t/edit') . '" alt="'.$stredit.'" class="iconsmall" /></a> ';
188     /// Delete
189     /// Can only delete the last category if there are no fields in it
190     if ( ($categorycount > 1) or ($fieldcount == 0) ) {
191         $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$category->id.'&amp;action=deletecategory';
192         $editstr .= '"><img src="'.$OUTPUT->pix_url('t/delete') . '" alt="'.$strdelete.'" class="iconsmall" /></a> ';
193     } else {
194         $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
195     }
197     /// Move up
198     if ($category->sortorder > 1) {
199         $editstr .= '<a title="'.$strmoveup.'" href="index.php?id='.$category->id.'&amp;action=movecategory&amp;dir=up&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/up') . '" alt="'.$strmoveup.'" class="iconsmall" /></a> ';
200     } else {
201         $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
202     }
204     /// Move down
205     if ($category->sortorder < $categorycount) {
206         $editstr .= '<a title="'.$strmovedown.'" href="index.php?id='.$category->id.'&amp;action=movecategory&amp;dir=down&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/down') . '" alt="'.$strmovedown.'" class="iconsmall" /></a> ';
207     } else {
208         $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
209     }
211     return $editstr;
214 /**
215  * Create a string containing the editing icons for the user profile fields
216  * @param   object   the field object
217  * @return  string   the icon string
218  */
219 function profile_field_icons($field) {
220     global $CFG, $USER, $DB, $OUTPUT;
222     if (empty($str)) {
223         $strdelete   = get_string('delete');
224         $strmoveup   = get_string('moveup');
225         $strmovedown = get_string('movedown');
226         $stredit     = get_string('edit');
227     }
229     $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$field->categoryid));
230     $datacount  = $DB->count_records('user_info_data', array('fieldid'=>$field->id));
232     /// Edit
233     $editstr = '<a title="'.$stredit.'" href="index.php?id='.$field->id.'&amp;action=editfield"><img src="'.$OUTPUT->pix_url('t/edit') . '" alt="'.$stredit.'" class="iconsmall" /></a> ';
235     /// Delete
236     $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$field->id.'&amp;action=deletefield';
237     $editstr .= '"><img src="'.$OUTPUT->pix_url('t/delete') . '" alt="'.$strdelete.'" class="iconsmall" /></a> ';
239     /// Move up
240     if ($field->sortorder > 1) {
241         $editstr .= '<a title="'.$strmoveup.'" href="index.php?id='.$field->id.'&amp;action=movefield&amp;dir=up&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/up') . '" alt="'.$strmoveup.'" class="iconsmall" /></a> ';
242      } else {
243         $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
244     }
246     /// Move down
247     if ($field->sortorder < $fieldcount) {
248         $editstr .= '<a title="'.$strmovedown.'" href="index.php?id='.$field->id.'&amp;action=movefield&amp;dir=down&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/down') . '" alt="'.$strmovedown.'" class="iconsmall" /></a> ';
249     } else {
250         $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
251     }
253     return $editstr;