PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/user/profile/index.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 272 lines | 173 code | 49 blank | 50 comment | 20 complexity | bf6d720b6f0f6655f6b0a0c24617190a MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Manage user profile fields.
  18. * @package core_user
  19. * @copyright 2007 onwards Shane Elliot {@link http://pukunui.com}
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. require('../../config.php');
  23. require_once($CFG->libdir.'/adminlib.php');
  24. require_once($CFG->dirroot.'/user/profile/lib.php');
  25. require_once($CFG->dirroot.'/user/profile/definelib.php');
  26. admin_externalpage_setup('profilefields');
  27. $action = optional_param('action', '', PARAM_ALPHA);
  28. $redirect = $CFG->wwwroot.'/user/profile/index.php';
  29. $strchangessaved = get_string('changessaved');
  30. $strcancelled = get_string('cancelled');
  31. $strdefaultcategory = get_string('profiledefaultcategory', 'admin');
  32. $strnofields = get_string('profilenofieldsdefined', 'admin');
  33. $strcreatefield = get_string('profilecreatefield', 'admin');
  34. // Do we have any actions to perform before printing the header.
  35. switch ($action) {
  36. case 'movecategory':
  37. $id = required_param('id', PARAM_INT);
  38. $dir = required_param('dir', PARAM_ALPHA);
  39. if (confirm_sesskey()) {
  40. profile_move_category($id, $dir);
  41. }
  42. redirect($redirect);
  43. break;
  44. case 'movefield':
  45. $id = required_param('id', PARAM_INT);
  46. $dir = required_param('dir', PARAM_ALPHA);
  47. if (confirm_sesskey()) {
  48. profile_move_field($id, $dir);
  49. }
  50. redirect($redirect);
  51. break;
  52. case 'deletecategory':
  53. $id = required_param('id', PARAM_INT);
  54. if (confirm_sesskey()) {
  55. profile_delete_category($id);
  56. }
  57. redirect($redirect, get_string('deleted'));
  58. break;
  59. case 'deletefield':
  60. $id = required_param('id', PARAM_INT);
  61. $confirm = optional_param('confirm', 0, PARAM_BOOL);
  62. // If no userdata for profile than don't show confirmation.
  63. $datacount = $DB->count_records('user_info_data', array('fieldid' => $id));
  64. if (((data_submitted() and $confirm) or ($datacount === 0)) and confirm_sesskey()) {
  65. profile_delete_field($id);
  66. redirect($redirect, get_string('deleted'));
  67. }
  68. // Ask for confirmation, as there is user data available for field.
  69. $fieldname = $DB->get_field('user_info_field', 'name', array('id' => $id));
  70. $optionsyes = array ('id' => $id, 'confirm' => 1, 'action' => 'deletefield', 'sesskey' => sesskey());
  71. $strheading = get_string('profiledeletefield', 'admin', format_string($fieldname));
  72. $PAGE->navbar->add($strheading);
  73. echo $OUTPUT->header();
  74. echo $OUTPUT->heading($strheading);
  75. $formcontinue = new single_button(new moodle_url($redirect, $optionsyes), get_string('yes'), 'post');
  76. $formcancel = new single_button(new moodle_url($redirect), get_string('no'), 'get');
  77. echo $OUTPUT->confirm(get_string('profileconfirmfielddeletion', 'admin', $datacount), $formcontinue, $formcancel);
  78. echo $OUTPUT->footer();
  79. die;
  80. break;
  81. case 'editfield':
  82. $id = optional_param('id', 0, PARAM_INT);
  83. $datatype = optional_param('datatype', '', PARAM_ALPHA);
  84. profile_edit_field($id, $datatype, $redirect);
  85. die;
  86. break;
  87. case 'editcategory':
  88. $id = optional_param('id', 0, PARAM_INT);
  89. profile_edit_category($id, $redirect);
  90. die;
  91. break;
  92. default:
  93. // Normal form.
  94. }
  95. // Show all categories.
  96. $categories = $DB->get_records('user_info_category', null, 'sortorder ASC');
  97. // Check that we have at least one category defined.
  98. if (empty($categories)) {
  99. $defaultcategory = new stdClass();
  100. $defaultcategory->name = $strdefaultcategory;
  101. $defaultcategory->sortorder = 1;
  102. $DB->insert_record('user_info_category', $defaultcategory);
  103. redirect($redirect);
  104. }
  105. // Print the header.
  106. echo $OUTPUT->header();
  107. echo $OUTPUT->heading(get_string('profilefields', 'admin'));
  108. foreach ($categories as $category) {
  109. $table = new html_table();
  110. $table->head = array(get_string('profilefield', 'admin'), get_string('edit'));
  111. $table->align = array('left', 'right');
  112. $table->width = '95%';
  113. $table->attributes['class'] = 'generaltable profilefield';
  114. $table->data = array();
  115. if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
  116. foreach ($fields as $field) {
  117. $table->data[] = array(format_string($field->name), profile_field_icons($field));
  118. }
  119. }
  120. echo $OUTPUT->heading(format_string($category->name) .' '.profile_category_icons($category));
  121. if (count($table->data)) {
  122. echo html_writer::table($table);
  123. } else {
  124. echo $OUTPUT->notification($strnofields);
  125. }
  126. } // End of $categories foreach.
  127. echo '<hr />';
  128. echo '<div class="profileeditor">';
  129. // Create a new field link.
  130. $options = profile_list_datatypes();
  131. $popupurl = new moodle_url('/user/profile/index.php?id=0&action=editfield');
  132. echo $OUTPUT->single_select($popupurl, 'datatype', $options, '', array('' => get_string('choosedots')), 'newfieldform', array('label' => $strcreatefield));
  133. // Add a div with a class so themers can hide, style or reposition the text.
  134. html_writer::start_tag('div', array('class' => 'adminuseractionhint'));
  135. echo get_string('or', 'lesson');
  136. html_writer::end_tag('div');
  137. // Create a new category link.
  138. $options = array('action' => 'editcategory');
  139. echo $OUTPUT->single_button(new moodle_url('index.php', $options), get_string('profilecreatecategory', 'admin'));
  140. echo '</div>';
  141. echo $OUTPUT->footer();
  142. die;
  143. /***** Some functions relevant to this script *****/
  144. /**
  145. * Create a string containing the editing icons for the user profile categories
  146. * @param stdClass $category the category object
  147. * @return string the icon string
  148. */
  149. function profile_category_icons($category) {
  150. global $CFG, $USER, $DB, $OUTPUT;
  151. $strdelete = get_string('delete');
  152. $strmoveup = get_string('moveup');
  153. $strmovedown = get_string('movedown');
  154. $stredit = get_string('edit');
  155. $categorycount = $DB->count_records('user_info_category');
  156. $fieldcount = $DB->count_records('user_info_field', array('categoryid' => $category->id));
  157. // Edit.
  158. $editstr = '<a title="'.$stredit.'" href="index.php?id='.$category->id.'&amp;action=editcategory">' .
  159. $OUTPUT->pix_icon('t/edit', $stredit) .'</a> ';
  160. // Delete.
  161. // Can only delete the last category if there are no fields in it.
  162. if (($categorycount > 1) or ($fieldcount == 0)) {
  163. $editstr .= '<a title="'.$strdelete.'"';
  164. $editstr .= ' href="index.php?id='.$category->id.'&amp;action=deletecategory&amp;sesskey='.sesskey() . '">';
  165. $editstr .= $OUTPUT->pix_icon('t/delete', $strdelete).'</a> ';
  166. } else {
  167. $editstr .= $OUTPUT->spacer() . ' ';
  168. }
  169. // Move up.
  170. if ($category->sortorder > 1) {
  171. $editstr .= '<a title="'.$strmoveup.'" ';
  172. $editstr .= ' href="index.php?id='.$category->id.'&amp;action=movecategory&amp;dir=up&amp;sesskey='.sesskey().'">';
  173. $editstr .= $OUTPUT->pix_icon('t/up', $strmoveup) . '</a> ';
  174. } else {
  175. $editstr .= $OUTPUT->spacer() . ' ';
  176. }
  177. // Move down.
  178. if ($category->sortorder < $categorycount) {
  179. $editstr .= '<a title="'.$strmovedown.'" ';
  180. $editstr .= ' href="index.php?id='.$category->id.'&amp;action=movecategory&amp;dir=down&amp;sesskey='.sesskey().'">';
  181. $editstr .= $OUTPUT->pix_icon('t/down', $strmovedown) . '</a> ';
  182. } else {
  183. $editstr .= $OUTPUT->spacer() . ' ';
  184. }
  185. return $editstr;
  186. }
  187. /**
  188. * Create a string containing the editing icons for the user profile fields
  189. * @param stdClass $field the field object
  190. * @return string the icon string
  191. */
  192. function profile_field_icons($field) {
  193. global $CFG, $USER, $DB, $OUTPUT;
  194. $strdelete = get_string('delete');
  195. $strmoveup = get_string('moveup');
  196. $strmovedown = get_string('movedown');
  197. $stredit = get_string('edit');
  198. $fieldcount = $DB->count_records('user_info_field', array('categoryid' => $field->categoryid));
  199. $datacount = $DB->count_records('user_info_data', array('fieldid' => $field->id));
  200. // Edit.
  201. $editstr = '<a title="'.$stredit.'" href="index.php?id='.$field->id.'&amp;action=editfield">';
  202. $editstr .= $OUTPUT->pix_icon('t/edit', $stredit) . '</a> ';
  203. // Delete.
  204. $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$field->id.'&amp;action=deletefield&amp;sesskey='.sesskey().'">';
  205. $editstr .= $OUTPUT->pix_icon('t/delete', $strdelete) . '</a> ';
  206. // Move up.
  207. if ($field->sortorder > 1) {
  208. $editstr .= '<a title="'.$strmoveup.'" ';
  209. $editstr .= ' href="index.php?id='.$field->id.'&amp;action=movefield&amp;dir=up&amp;sesskey='.sesskey().'">';
  210. $editstr .= $OUTPUT->pix_icon('t/up', $strmoveup) . '</a> ';
  211. } else {
  212. $editstr .= $OUTPUT->spacer() . ' ';
  213. }
  214. // Move down.
  215. if ($field->sortorder < $fieldcount) {
  216. $editstr .= '<a title="'.$strmovedown.'" ';
  217. $editstr .= ' href="index.php?id='.$field->id.'&amp;action=movefield&amp;dir=down&amp;sesskey='.sesskey().'">';
  218. $editstr .= $OUTPUT->pix_icon('t/down', $strmovedown) . '</a> ';
  219. } else {
  220. $editstr .= $OUTPUT->spacer() . ' ';
  221. }
  222. return $editstr;
  223. }