[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/user/profile/ -> definelib.php (source)

   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   * This file contains the profile_define_base class.
  19   *
  20   * @package core_user
  21   * @copyright  2007 onwards Shane Elliot {@link http://pukunui.com}
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Class profile_define_base
  27   *
  28   * @copyright  2007 onwards Shane Elliot {@link http://pukunui.com}
  29   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class profile_define_base {
  32  
  33      /**
  34       * Prints out the form snippet for creating or editing a profile field
  35       * @param moodleform $form instance of the moodleform class
  36       */
  37      public function define_form(&$form) {
  38          $form->addElement('header', '_commonsettings', get_string('profilecommonsettings', 'admin'));
  39          $this->define_form_common($form);
  40  
  41          $form->addElement('header', '_specificsettings', get_string('profilespecificsettings', 'admin'));
  42          $this->define_form_specific($form);
  43      }
  44  
  45      /**
  46       * Prints out the form snippet for the part of creating or editing a profile field common to all data types.
  47       *
  48       * @param moodleform $form instance of the moodleform class
  49       */
  50      public function define_form_common(&$form) {
  51  
  52          $strrequired = get_string('required');
  53  
  54          $form->addElement('text', 'shortname', get_string('profileshortname', 'admin'), 'maxlength="100" size="25"');
  55          $form->addRule('shortname', $strrequired, 'required', null, 'client');
  56          $form->setType('shortname', PARAM_ALPHANUM);
  57  
  58          $form->addElement('text', 'name', get_string('profilename', 'admin'), 'size="50"');
  59          $form->addRule('name', $strrequired, 'required', null, 'client');
  60          $form->setType('name', PARAM_TEXT);
  61  
  62          $form->addElement('editor', 'description', get_string('profiledescription', 'admin'), null, null);
  63  
  64          $form->addElement('selectyesno', 'required', get_string('profilerequired', 'admin'));
  65  
  66          $form->addElement('selectyesno', 'locked', get_string('profilelocked', 'admin'));
  67  
  68          $form->addElement('selectyesno', 'forceunique', get_string('profileforceunique', 'admin'));
  69  
  70          $form->addElement('selectyesno', 'signup', get_string('profilesignup', 'admin'));
  71  
  72          $choices = array();
  73          $choices[PROFILE_VISIBLE_NONE]    = get_string('profilevisiblenone', 'admin');
  74          $choices[PROFILE_VISIBLE_PRIVATE] = get_string('profilevisibleprivate', 'admin');
  75          $choices[PROFILE_VISIBLE_ALL]     = get_string('profilevisibleall', 'admin');
  76          $form->addElement('select', 'visible', get_string('profilevisible', 'admin'), $choices);
  77          $form->addHelpButton('visible', 'profilevisible', 'admin');
  78          $form->setDefault('visible', PROFILE_VISIBLE_ALL);
  79  
  80          $choices = profile_list_categories();
  81          $form->addElement('select', 'categoryid', get_string('profilecategory', 'admin'), $choices);
  82      }
  83  
  84      /**
  85       * Prints out the form snippet for the part of creating or editing a profile field specific to the current data type.
  86       * @param moodleform $form instance of the moodleform class
  87       */
  88      public function define_form_specific($form) {
  89          // Do nothing - overwrite if necessary.
  90      }
  91  
  92      /**
  93       * Validate the data from the add/edit profile field form.
  94       *
  95       * Generally this method should not be overwritten by child classes.
  96       *
  97       * @param stdClass|array $data from the add/edit profile field form
  98       * @param array $files
  99       * @return array associative array of error messages
 100       */
 101      public function define_validate($data, $files) {
 102  
 103          $data = (object)$data;
 104          $err = array();
 105  
 106          $err += $this->define_validate_common($data, $files);
 107          $err += $this->define_validate_specific($data, $files);
 108  
 109          return $err;
 110      }
 111  
 112      /**
 113       * Validate the data from the add/edit profile field form that is common to all data types.
 114       *
 115       * Generally this method should not be overwritten by child classes.
 116       *
 117       * @param stdClass|array $data from the add/edit profile field form
 118       * @param array $files
 119       * @return  array    associative array of error messages
 120       */
 121      public function define_validate_common($data, $files) {
 122          global $DB;
 123  
 124          $err = array();
 125  
 126          // Check the shortname was not truncated by cleaning.
 127          if (empty($data->shortname)) {
 128              $err['shortname'] = get_string('required');
 129  
 130          } else {
 131              // Fetch field-record from DB.
 132              $field = $DB->get_record('user_info_field', array('shortname' => $data->shortname));
 133              // Check the shortname is unique.
 134              if ($field and $field->id <> $data->id) {
 135                  $err['shortname'] = get_string('profileshortnamenotunique', 'admin');
 136              }
 137              // NOTE: since 2.0 the shortname may collide with existing fields in $USER because we load these fields into
 138              // $USER->profile array instead.
 139          }
 140  
 141          // No further checks necessary as the form class will take care of it.
 142          return $err;
 143      }
 144  
 145      /**
 146       * Validate the data from the add/edit profile field form
 147       * that is specific to the current data type
 148       * @param array $data
 149       * @param array $files
 150       * @return  array    associative array of error messages
 151       */
 152      public function define_validate_specific($data, $files) {
 153          // Do nothing - overwrite if necessary.
 154          return array();
 155      }
 156  
 157      /**
 158       * Alter form based on submitted or existing data
 159       * @param moodleform $mform
 160       */
 161      public function define_after_data(&$mform) {
 162          // Do nothing - overwrite if necessary.
 163      }
 164  
 165      /**
 166       * Add a new profile field or save changes to current field
 167       * @param array|stdClass $data from the add/edit profile field form
 168       */
 169      public function define_save($data) {
 170          global $DB;
 171  
 172          $data = $this->define_save_preprocess($data); // Hook for child classes.
 173  
 174          $old = false;
 175          if (!empty($data->id)) {
 176              $old = $DB->get_record('user_info_field', array('id' => (int)$data->id));
 177          }
 178  
 179          // Check to see if the category has changed.
 180          if (!$old or $old->categoryid != $data->categoryid) {
 181              $data->sortorder = $DB->count_records('user_info_field', array('categoryid' => $data->categoryid)) + 1;
 182          }
 183  
 184          if (empty($data->id)) {
 185              unset($data->id);
 186              $data->id = $DB->insert_record('user_info_field', $data);
 187          } else {
 188              $DB->update_record('user_info_field', $data);
 189          }
 190      }
 191  
 192      /**
 193       * Preprocess data from the add/edit profile field form before it is saved.
 194       *
 195       * This method is a hook for the child classes to overwrite.
 196       *
 197       * @param array|stdClass $data from the add/edit profile field form
 198       * @return array|stdClass processed data object
 199       */
 200      public function define_save_preprocess($data) {
 201          // Do nothing - overwrite if necessary.
 202          return $data;
 203      }
 204  
 205      /**
 206       * Provides a method by which we can allow the default data in profile_define_* to use an editor
 207       *
 208       * This should return an array of editor names (which will need to be formatted/cleaned)
 209       *
 210       * @return array
 211       */
 212      public function define_editors() {
 213          return array();
 214      }
 215  }
 216  
 217  
 218  
 219  /**
 220   * Reorder the profile fields within a given category starting at the field at the given startorder.
 221   */
 222  function profile_reorder_fields() {
 223      global $DB;
 224  
 225      if ($categories = $DB->get_records('user_info_category')) {
 226          foreach ($categories as $category) {
 227              $i = 1;
 228              if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
 229                  foreach ($fields as $field) {
 230                      $f = new stdClass();
 231                      $f->id = $field->id;
 232                      $f->sortorder = $i++;
 233                      $DB->update_record('user_info_field', $f);
 234                  }
 235              }
 236          }
 237      }
 238  }
 239  
 240  /**
 241   * Reorder the profile categoriess starting at the category at the given startorder.
 242   */
 243  function profile_reorder_categories() {
 244      global $DB;
 245  
 246      $i = 1;
 247      if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
 248          foreach ($categories as $cat) {
 249              $c = new stdClass();
 250              $c->id = $cat->id;
 251              $c->sortorder = $i++;
 252              $DB->update_record('user_info_category', $c);
 253          }
 254      }
 255  }
 256  
 257  /**
 258   * Delete a profile category
 259   * @param int $id of the category to be deleted
 260   * @return bool success of operation
 261   */
 262  function profile_delete_category($id) {
 263      global $DB;
 264  
 265      // Retrieve the category.
 266      if (!$category = $DB->get_record('user_info_category', array('id' => $id))) {
 267          print_error('invalidcategoryid');
 268      }
 269  
 270      if (!$categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
 271          print_error('nocate', 'debug');
 272      }
 273  
 274      unset($categories[$category->id]);
 275  
 276      if (!count($categories)) {
 277          return false; // We can not delete the last category.
 278      }
 279  
 280      // Does the category contain any fields.
 281      if ($DB->count_records('user_info_field', array('categoryid' => $category->id))) {
 282          if (array_key_exists($category->sortorder - 1, $categories)) {
 283              $newcategory = $categories[$category->sortorder - 1];
 284          } else if (array_key_exists($category->sortorder + 1, $categories)) {
 285              $newcategory = $categories[$category->sortorder + 1];
 286          } else {
 287              $newcategory = reset($categories); // Get first category if sortorder broken.
 288          }
 289  
 290          $sortorder = $DB->count_records('user_info_field', array('categoryid' => $newcategory->id)) + 1;
 291  
 292          if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
 293              foreach ($fields as $field) {
 294                  $f = new stdClass();
 295                  $f->id = $field->id;
 296                  $f->sortorder = $sortorder++;
 297                  $f->categoryid = $newcategory->id;
 298                  $DB->update_record('user_info_field', $f);
 299              }
 300          }
 301      }
 302  
 303      // Finally we get to delete the category.
 304      $DB->delete_records('user_info_category', array('id' => $category->id));
 305      profile_reorder_categories();
 306      return true;
 307  }
 308  
 309  /**
 310   * Deletes a profile field.
 311   * @param int $id
 312   */
 313  function profile_delete_field($id) {
 314      global $DB;
 315  
 316      // Remove any user data associated with this field.
 317      if (!$DB->delete_records('user_info_data', array('fieldid' => $id))) {
 318          print_error('cannotdeletecustomfield');
 319      }
 320  
 321      // Note: Any availability conditions that depend on this field will remain,
 322      // but show the field as missing until manually corrected to something else.
 323  
 324      // Need to rebuild course cache to update the info.
 325      rebuild_course_cache(0, true);
 326  
 327      // Try to remove the record from the database.
 328      $DB->delete_records('user_info_field', array('id' => $id));
 329  
 330      // Reorder the remaining fields in the same category.
 331      profile_reorder_fields();
 332  }
 333  
 334  /**
 335   * Change the sort order of a field
 336   *
 337   * @param int $id of the field
 338   * @param string $move direction of move
 339   * @return bool success of operation
 340   */
 341  function profile_move_field($id, $move) {
 342      global $DB;
 343  
 344      // Get the field object.
 345      if (!$field = $DB->get_record('user_info_field', array('id' => $id), 'id, sortorder, categoryid')) {
 346          return false;
 347      }
 348      // Count the number of fields in this category.
 349      $fieldcount = $DB->count_records('user_info_field', array('categoryid' => $field->categoryid));
 350  
 351      // Calculate the new sortorder.
 352      if ( ($move == 'up') and ($field->sortorder > 1)) {
 353          $neworder = $field->sortorder - 1;
 354      } else if (($move == 'down') and ($field->sortorder < $fieldcount)) {
 355          $neworder = $field->sortorder + 1;
 356      } else {
 357          return false;
 358      }
 359  
 360      // Retrieve the field object that is currently residing in the new position.
 361      $params = array('categoryid' => $field->categoryid, 'sortorder' => $neworder);
 362      if ($swapfield = $DB->get_record('user_info_field', $params, 'id, sortorder')) {
 363  
 364          // Swap the sortorders.
 365          $swapfield->sortorder = $field->sortorder;
 366          $field->sortorder     = $neworder;
 367  
 368          // Update the field records.
 369          $DB->update_record('user_info_field', $field);
 370          $DB->update_record('user_info_field', $swapfield);
 371      }
 372  
 373      profile_reorder_fields();
 374      return true;
 375  }
 376  
 377  /**
 378   * Change the sort order of a category.
 379   *
 380   * @param int $id of the category
 381   * @param string $move direction of move
 382   * @return bool success of operation
 383   */
 384  function profile_move_category($id, $move) {
 385      global $DB;
 386      // Get the category object.
 387      if (!($category = $DB->get_record('user_info_category', array('id' => $id), 'id, sortorder'))) {
 388          return false;
 389      }
 390  
 391      // Count the number of categories.
 392      $categorycount = $DB->count_records('user_info_category');
 393  
 394      // Calculate the new sortorder.
 395      if (($move == 'up') and ($category->sortorder > 1)) {
 396          $neworder = $category->sortorder - 1;
 397      } else if (($move == 'down') and ($category->sortorder < $categorycount)) {
 398          $neworder = $category->sortorder + 1;
 399      } else {
 400          return false;
 401      }
 402  
 403      // Retrieve the category object that is currently residing in the new position.
 404      if ($swapcategory = $DB->get_record('user_info_category', array('sortorder' => $neworder), 'id, sortorder')) {
 405  
 406          // Swap the sortorders.
 407          $swapcategory->sortorder = $category->sortorder;
 408          $category->sortorder     = $neworder;
 409  
 410          // Update the category records.
 411          $DB->update_record('user_info_category', $category) and $DB->update_record('user_info_category', $swapcategory);
 412          return true;
 413      }
 414  
 415      return false;
 416  }
 417  
 418  /**
 419   * Retrieve a list of all the available data types
 420   * @return   array   a list of the datatypes suitable to use in a select statement
 421   */
 422  function profile_list_datatypes() {
 423      $datatypes = array();
 424  
 425      $plugins = core_component::get_plugin_list('profilefield');
 426      foreach ($plugins as $type => $unused) {
 427          $datatypes[$type] = get_string('pluginname', 'profilefield_'.$type);
 428      }
 429      asort($datatypes);
 430  
 431      return $datatypes;
 432  }
 433  
 434  /**
 435   * Retrieve a list of categories and ids suitable for use in a form
 436   * @return   array
 437   */
 438  function profile_list_categories() {
 439      global $DB;
 440      if (!$categories = $DB->get_records_menu('user_info_category', null, 'sortorder ASC', 'id, name')) {
 441          $categories = array();
 442      }
 443      return $categories;
 444  }
 445  
 446  
 447  /**
 448   * Edit a category
 449   *
 450   * @param int $id
 451   * @param string $redirect
 452   */
 453  function profile_edit_category($id, $redirect) {
 454      global $DB, $OUTPUT, $CFG;
 455  
 456      require_once($CFG->dirroot.'/user/profile/index_category_form.php');
 457      $categoryform = new category_form();
 458  
 459      if ($category = $DB->get_record('user_info_category', array('id' => $id))) {
 460          $categoryform->set_data($category);
 461      }
 462  
 463      if ($categoryform->is_cancelled()) {
 464          redirect($redirect);
 465      } else {
 466          if ($data = $categoryform->get_data()) {
 467              if (empty($data->id)) {
 468                  unset($data->id);
 469                  $data->sortorder = $DB->count_records('user_info_category') + 1;
 470                  $DB->insert_record('user_info_category', $data, false);
 471              } else {
 472                  $DB->update_record('user_info_category', $data);
 473              }
 474              profile_reorder_categories();
 475              redirect($redirect);
 476  
 477          }
 478  
 479          if (empty($id)) {
 480              $strheading = get_string('profilecreatenewcategory', 'admin');
 481          } else {
 482              $strheading = get_string('profileeditcategory', 'admin', format_string($category->name));
 483          }
 484  
 485          // Print the page.
 486          echo $OUTPUT->header();
 487          echo $OUTPUT->heading($strheading);
 488          $categoryform->display();
 489          echo $OUTPUT->footer();
 490          die;
 491      }
 492  
 493  }
 494  
 495  /**
 496   * Edit a profile field.
 497   *
 498   * @param int $id
 499   * @param string $datatype
 500   * @param string $redirect
 501   */
 502  function profile_edit_field($id, $datatype, $redirect) {
 503      global $CFG, $DB, $OUTPUT, $PAGE;
 504  
 505      if (!$field = $DB->get_record('user_info_field', array('id' => $id))) {
 506          $field = new stdClass();
 507          $field->datatype = $datatype;
 508          $field->description = '';
 509          $field->descriptionformat = FORMAT_HTML;
 510          $field->defaultdata = '';
 511          $field->defaultdataformat = FORMAT_HTML;
 512      }
 513  
 514      // Clean and prepare description for the editor.
 515      $field->description = clean_text($field->description, $field->descriptionformat);
 516      $field->description = array('text' => $field->description, 'format' => $field->descriptionformat, 'itemid' => 0);
 517  
 518      require_once($CFG->dirroot.'/user/profile/index_field_form.php');
 519      $fieldform = new field_form(null, $field->datatype);
 520  
 521      // Convert the data format for.
 522      if (is_array($fieldform->editors())) {
 523          foreach ($fieldform->editors() as $editor) {
 524              if (isset($field->$editor)) {
 525                  $field->$editor = clean_text($field->$editor, $field->{$editor.'format'});
 526                  $field->$editor = array('text' => $field->$editor, 'format' => $field->{$editor.'format'}, 'itemid' => 0);
 527              }
 528          }
 529      }
 530  
 531      $fieldform->set_data($field);
 532  
 533      if ($fieldform->is_cancelled()) {
 534          redirect($redirect);
 535  
 536      } else {
 537          if ($data = $fieldform->get_data()) {
 538              require_once($CFG->dirroot.'/user/profile/field/'.$datatype.'/define.class.php');
 539              $newfield = 'profile_define_'.$datatype;
 540              $formfield = new $newfield();
 541  
 542              // Collect the description and format back into the proper data structure from the editor.
 543              // Note: This field will ALWAYS be an editor.
 544              $data->descriptionformat = $data->description['format'];
 545              $data->description = $data->description['text'];
 546  
 547              // Check whether the default data is an editor, this is (currently) only the textarea field type.
 548              if (is_array($data->defaultdata) && array_key_exists('text', $data->defaultdata)) {
 549                  // Collect the default data and format back into the proper data structure from the editor.
 550                  $data->defaultdataformat = $data->defaultdata['format'];
 551                  $data->defaultdata = $data->defaultdata['text'];
 552              }
 553  
 554              // Convert the data format for.
 555              if (is_array($fieldform->editors())) {
 556                  foreach ($fieldform->editors() as $editor) {
 557                      if (isset($field->$editor)) {
 558                          $field->{$editor.'format'} = $field->{$editor}['format'];
 559                          $field->$editor = $field->{$editor}['text'];
 560                      }
 561                  }
 562              }
 563  
 564              $formfield->define_save($data);
 565              profile_reorder_fields();
 566              profile_reorder_categories();
 567              redirect($redirect);
 568          }
 569  
 570          $datatypes = profile_list_datatypes();
 571  
 572          if (empty($id)) {
 573              $strheading = get_string('profilecreatenewfield', 'admin', $datatypes[$datatype]);
 574          } else {
 575              $strheading = get_string('profileeditfield', 'admin', $field->name);
 576          }
 577  
 578          // Print the page.
 579          $PAGE->navbar->add($strheading);
 580          echo $OUTPUT->header();
 581          echo $OUTPUT->heading($strheading);
 582          $fieldform->display();
 583          echo $OUTPUT->footer();
 584          die;
 585      }
 586  }
 587  
 588  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1