Search moodle.org's
Developer Documentation

   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  /**
  18   * Manage user profile fields.
  19   * @package core_user
  20   * @copyright  2007 onwards Shane Elliot {@link http://pukunui.com}
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  require('../../config.php');
  25  require_once($CFG->libdir.'/adminlib.php');
  26  require_once($CFG->dirroot.'/user/profile/lib.php');
  27  require_once($CFG->dirroot.'/user/profile/definelib.php');
  28  
  29  admin_externalpage_setup('profilefields');
  30  
  31  $action   = optional_param('action', '', PARAM_ALPHA);
  32  
  33  $redirect = $CFG->wwwroot.'/user/profile/index.php';
  34  
  35  $strchangessaved    = get_string('changessaved');
  36  $strcancelled       = get_string('cancelled');
  37  $strdefaultcategory = get_string('profiledefaultcategory', 'admin');
  38  $strnofields        = get_string('profilenofieldsdefined', 'admin');
  39  $strcreatefield     = get_string('profilecreatefield', 'admin');
  40  
  41  
  42  // Do we have any actions to perform before printing the header.
  43  
  44  switch ($action) {
  45      case 'movecategory':
  46          $id  = required_param('id', PARAM_INT);
  47          $dir = required_param('dir', PARAM_ALPHA);
  48  
  49          if (confirm_sesskey()) {
  50              profile_move_category($id, $dir);
  51          }
  52          redirect($redirect);
  53          break;
  54      case 'movefield':
  55          $id  = required_param('id', PARAM_INT);
  56          $dir = required_param('dir', PARAM_ALPHA);
  57  
  58          if (confirm_sesskey()) {
  59              profile_move_field($id, $dir);
  60          }
  61          redirect($redirect);
  62          break;
  63      case 'deletecategory':
  64          $id      = required_param('id', PARAM_INT);
  65          if (confirm_sesskey()) {
  66              profile_delete_category($id);
  67          }
  68          redirect($redirect, get_string('deleted'));
  69          break;
  70      case 'deletefield':
  71          $id      = required_param('id', PARAM_INT);
  72          $confirm = optional_param('confirm', 0, PARAM_BOOL);
  73  
  74          // If no userdata for profile than don't show confirmation.
  75          $datacount = $DB->count_records('user_info_data', array('fieldid' => $id));
  76          if (((data_submitted() and $confirm) or ($datacount === 0)) and confirm_sesskey()) {
  77              profile_delete_field($id);
  78              redirect($redirect, get_string('deleted'));
  79          }
  80  
  81          // Ask for confirmation, as there is user data available for field.
  82          $fieldname = $DB->get_field('user_info_field', 'name', array('id' => $id));
  83          $optionsyes = array ('id' => $id, 'confirm' => 1, 'action' => 'deletefield', 'sesskey' => sesskey());
  84          $strheading = get_string('profiledeletefield', 'admin', $fieldname);
  85          $PAGE->navbar->add($strheading);
  86          echo $OUTPUT->header();
  87          echo $OUTPUT->heading($strheading);
  88          $formcontinue = new single_button(new moodle_url($redirect, $optionsyes), get_string('yes'), 'post');
  89          $formcancel = new single_button(new moodle_url($redirect), get_string('no'), 'get');
  90          echo $OUTPUT->confirm(get_string('profileconfirmfielddeletion', 'admin', $datacount), $formcontinue, $formcancel);
  91          echo $OUTPUT->footer();
  92          die;
  93          break;
  94      case 'editfield':
  95          $id       = optional_param('id', 0, PARAM_INT);
  96          $datatype = optional_param('datatype', '', PARAM_ALPHA);
  97  
  98          profile_edit_field($id, $datatype, $redirect);
  99          die;
 100          break;
 101      case 'editcategory':
 102          $id = optional_param('id', 0, PARAM_INT);
 103  
 104          profile_edit_category($id, $redirect);
 105          die;
 106          break;
 107      default:
 108          // Normal form.
 109  }
 110  
 111  // Show all categories.
 112  $categories = $DB->get_records('user_info_category', null, 'sortorder ASC');
 113  
 114  // Check that we have at least one category defined.
 115  if (empty($categories)) {
 116      $defaultcategory = new stdClass();
 117      $defaultcategory->name = $strdefaultcategory;
 118      $defaultcategory->sortorder = 1;
 119      $DB->insert_record('user_info_category', $defaultcategory);
 120      redirect($redirect);
 121  }
 122  
 123  // Print the header.
 124  echo $OUTPUT->header();
 125  echo $OUTPUT->heading(get_string('profilefields', 'admin'));
 126  
 127  foreach ($categories as $category) {
 128      $table = new html_table();
 129      $table->head  = array(get_string('profilefield', 'admin'), get_string('edit'));
 130      $table->align = array('left', 'right');
 131      $table->width = '95%';
 132      $table->attributes['class'] = 'generaltable profilefield';
 133      $table->data = array();
 134  
 135      if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
 136          foreach ($fields as $field) {
 137              $table->data[] = array(format_string($field->name), profile_field_icons($field));
 138          }
 139      }
 140  
 141      echo $OUTPUT->heading(format_string($category->name) .' '.profile_category_icons($category));
 142      if (count($table->data)) {
 143          echo html_writer::table($table);
 144      } else {
 145          echo $OUTPUT->notification($strnofields);
 146      }
 147  
 148  } // End of $categories foreach.
 149  
 150  echo '<hr />';
 151  echo '<div class="profileeditor">';
 152  
 153  // Create a new field link.
 154  $options = profile_list_datatypes();
 155  $popupurl = new moodle_url('/user/profile/index.php?id=0&action=editfield');
 156  echo $OUTPUT->single_select($popupurl, 'datatype', $options, '', array('' => $strcreatefield), 'newfieldform');
 157  
 158  // Add a div with a class so themers can hide, style or reposition the text.
 159  html_writer::start_tag('div', array('class' => 'adminuseractionhint'));
 160  echo get_string('or', 'lesson');
 161  html_writer::end_tag('div');
 162  
 163  // Create a new category link.
 164  $options = array('action' => 'editcategory');
 165  echo $OUTPUT->single_button(new moodle_url('index.php', $options), get_string('profilecreatecategory', 'admin'));
 166  
 167  echo '</div>';
 168  
 169  echo $OUTPUT->footer();
 170  die;
 171  
 172  
 173  /***** Some functions relevant to this script *****/
 174  
 175  /**
 176   * Create a string containing the editing icons for the user profile categories
 177   * @param stdClass $category the category object
 178   * @return string the icon string
 179   */
 180  function profile_category_icons($category) {
 181      global $CFG, $USER, $DB, $OUTPUT;
 182  
 183      $strdelete   = get_string('delete');
 184      $strmoveup   = get_string('moveup');
 185      $strmovedown = get_string('movedown');
 186      $stredit     = get_string('edit');
 187  
 188      $categorycount = $DB->count_records('user_info_category');
 189      $fieldcount    = $DB->count_records('user_info_field', array('categoryid' => $category->id));
 190  
 191      // Edit.
 192      $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> ';
 193  
 194      // Delete.
 195      // Can only delete the last category if there are no fields in it.
 196      if (($categorycount > 1) or ($fieldcount == 0)) {
 197          $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$category->id.'&amp;action=deletecategory&amp;sesskey='.sesskey();
 198          $editstr .= '"><img src="'.$OUTPUT->pix_url('t/delete') . '" alt="'.$strdelete.'" class="iconsmall" /></a> ';
 199      } else {
 200          $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
 201      }
 202  
 203      // Move up.
 204      if ($category->sortorder > 1) {
 205          $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> ';
 206      } else {
 207          $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
 208      }
 209  
 210      // Move down.
 211      if ($category->sortorder < $categorycount) {
 212          $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> ';
 213      } else {
 214          $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
 215      }
 216  
 217      return $editstr;
 218  }
 219  
 220  /**
 221   * Create a string containing the editing icons for the user profile fields
 222   * @param stdClass $field the field object
 223   * @return string the icon string
 224   */
 225  function profile_field_icons($field) {
 226      global $CFG, $USER, $DB, $OUTPUT;
 227  
 228      $strdelete   = get_string('delete');
 229      $strmoveup   = get_string('moveup');
 230      $strmovedown = get_string('movedown');
 231      $stredit     = get_string('edit');
 232  
 233      $fieldcount = $DB->count_records('user_info_field', array('categoryid' => $field->categoryid));
 234      $datacount  = $DB->count_records('user_info_data', array('fieldid' => $field->id));
 235  
 236      // Edit.
 237      $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> ';
 238  
 239      // Delete.
 240      $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$field->id.'&amp;action=deletefield&amp;sesskey='.sesskey();
 241      $editstr .= '"><img src="'.$OUTPUT->pix_url('t/delete') . '" alt="'.$strdelete.'" class="iconsmall" /></a> ';
 242  
 243      // Move up.
 244      if ($field->sortorder > 1) {
 245          $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> ';
 246      } else {
 247          $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
 248      }
 249  
 250      // Move down.
 251      if ($field->sortorder < $fieldcount) {
 252          $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> ';
 253      } else {
 254          $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
 255      }
 256  
 257      return $editstr;
 258  }
 259  
 260  
 261  

Search This Site: