<commit_before><?php
/* SVN FILE: $Id$ */

/**
 * Enter description here ....
 *
 * @filesource
 * @copyright    Copyright (c) 2006, .
 * @link
 * @package
 * @subpackage
 * @since
 * @version      $Revision$
 * @modifiedby   $LastChangedBy$
 * @lastmodified $Date: 2006/07/07 00:02:41 $
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * User
 *
 * Enter description here...
 *
 * @package
 * @subpackage
 * @since
 */
App::import('Lib', 'neat_string');

class User extends AppModel
{
  //The model name
  var $name = 'User';
  var $displayField = 'full_name';
  var $unhashed_password = '';

  /* User Type - Admin, Instructor, TA, Student */
  var $USER_TYPE_ADMIN = 'A';
  var $USER_TYPE_INSTRUCTOR = 'I';
  var $USER_TYPE_TA = 'T';
  var $USER_TYPE_STUDENT = 'S';

  var $actsAs = array('ExtendAssociations', 'Containable', 'Habtamable');

  var $hasMany =array('Submission' => array('className' => 'EvaluationSubmission',
                                            'foreignKey' => 'submitter_id',
                                            'dependent' => true));
/*  var $hasMany = array('UserCourse' => array('className'   => 'UserCourse',
                                             'conditions'  => 'UserCourse.record_status = "A"',
                                             'order'       => 'UserCourse.course_id ASC',
                                             'limit'       => '99999',
                                             'foreignKey'  => 'user_id',
                                             'dependent'   => true,
                                             'exclusive'   => false,
                                             'finderSql'   => ''),
                       'UserEnrol' => array('className'   => 'UserEnrol',
                                            'conditions'  => 'UserEnrol.record_status = "A"',
                                            'order'       => 'UserEnrol.course_id ASC',
                                            'limit'       => '99999',
                                            'foreignKey'  => 'user_id',
                                            'dependent'   => true,
                                            'exclusive'   => false,
                                            'finderSql'   => '')
                      );*/

  var $hasAndBelongsToMany = array('Course' =>
                                   array('className'    => 'Course',
                                         'joinTable'    => 'user_courses',
                                         'foreignKey'   => 'user_id',
                                         'associationForeignKey'    =>  'course_id',
                                         'conditions'   => '',
                                         'order'        => '',
                                         'limit'        => '',
                                         'unique'       => true,
                                         'finderQuery'  => '',
                                         'deleteQuery'  => '',
                                         'dependent'    => true,
                                        ),
                                   'Enrolment' =>
                                   array('className'    => 'Course',
                                         'joinTable'    => 'user_enrols',
                                         'foreignKey'   => 'user_id',
                                         'associationForeignKey'    =>  'course_id',
                                         'conditions'   => '',
                                         'order'        => '',
                                         'limit'        => '',
                                         'unique'       => true,
                                         'finderQuery'  => '',
                                         'deleteQuery'  => '',
                                         'dependent'    => true,
                                        ),
                                   'Group' =>
                                   array('className'    => 'Group',
                                         'joinTable'    => 'groups_members',
                                         'foreignKey'   => 'user_id',
                                         'associationForeignKey'    =>  'group_id',
                                         'conditions'   => '',
                                         'order'        => '',
                                         'limit'        => '',
                                         'unique'       => true,
                                         'finderQuery'  => '',
                                         'deleteQuery'  => '',
                                         'dependent'    => true,
                                        ),
                                   'Role' =>
                                   array('className'    => 'Role',
                                         'joinTable'    => 'roles_users',
                                         'foreignKey'   => 'user_id',
                                         'associationForeignKey'    => 'role_id',
                                         'conditions'   => '',
                                         'order'        => '',
                                         'limit'        => '',
                                         'unique'       => true,
                                         'finderQuery'  => '',
                                         'deleteQuery'  => '',
                                         'dependent'    => true,
                                        )
                                   );

  var $validate = array('username'  => array('character' => array('rule' => 'alphaNumeric',
                                                                  'required' => true,
                                                                  'message' => 'Alphabets and numbers only'),
                                             'unique' => array('rule' => 'isUnique',
                                                               'messages' => 'Duplicate Username found. Please change the username.')),
                        'email'     => array('rule' => 'email',
                                             'required' => false,
                                             'allowEmpty' => true,
                                             'message' => 'Invalid email format'));

  function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);
    $this->virtualFields['full_name'] = sprintf('CONCAT(%s.first_name, " ", %s.last_name)', $this->alias, $this->alias);
    $this->virtualFields['student_no_with_full_name'] = sprintf('CONCAT(%s.student_no, " - ", %s.first_name, " ", %s.last_name)', $this->alias, $this->alias, $this->alias);
  }

  //Overwriting Function - will be called before save operation
  function beforeSave() {
    $allowSave = true;

    if(!isset($this->data[$this->name]['id']) && empty($this->data[$this->name]['password'])) {
      $this->data[$this->name]['password'] = <PASSWORD>);
    }

    // generate a password hash if it was set
    if (!empty($this->data[$this->name]['password'])) {
      $this->unhashed_password = $this->data[$this->name]['password'];
      $this->data[$this->name]['password'] = md5($this->data[$this->name]['password']);
    }

    // clear password to avoid updating to a empty one
    if(empty($this->data[$this->name]['password'])) {
      unset($this->data[$this->name]['password']);
    }

    return $allowSave && parent::beforeSave();
  }

  function afterSave(){
    $this->data[$this->name]['password'] = $this->unhashed_password;
  }

  function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
    if(!isset($fields['contain'])) {
    } elseif ($fields['contain'] === false) {
      $fields['contain'] = array('Creator', 'Updater');
    } else {
      $fields['contain'] = array_merge($fields['contain'], array('Creator', 'Updater'));
    }
    return parent::find($conditions, $fields, $order, $recursive);
  }

  //Validation check on duplication of username
  function hasDuplicateUsername($username) {
    if ($this->find('first', array('conditions' => array('username' => $username)))) {
      $this->errorMessage='Duplicate Username found. Please change the username of this user.';
      /*if ($this->data[$this->name]['role'] == 'S') {
        $this->errorMessage.='<br>If you want to enrol this student to one or more courses, use the enrol function on User Listing page.';
        }*/
      return false;
    }

    return true;<commit_msg>Fixed double password hash on profile edit.<commit_after><?php
/* SVN FILE: $Id$ */

/**
 * Enter description here ....
 *
 * @filesource
 * @copyright    Copyright (c) 2006, .
 * @link
 * @package
 * @subpackage
 * @since
 * @version      $Revision$
 * @modifiedby   $LastChangedBy$
 * @lastmodified $Date: 2006/07/07 00:02:41 $
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * User
 *
 * Enter description here...
 *
 * @package
 * @subpackage
 * @since
 */
App::import('Lib', 'neat_string');

class User extends AppModel
{
  //The model name
  var $name = 'User';
  var $displayField = 'full_name';
  var $unhashed_password = '';

  /* User Type - Admin, Instructor, TA, Student */
  var $USER_TYPE_ADMIN = 'A';
  var $USER_TYPE_INSTRUCTOR = 'I';
  var $USER_TYPE_TA = 'T';
  var $USER_TYPE_STUDENT = 'S';

  var $actsAs = array('ExtendAssociations', 'Containable', 'Habtamable');

  var $hasMany =array('Submission' => array('className' => 'EvaluationSubmission',
                                            'foreignKey' => 'submitter_id',
                                            'dependent' => true));
/*  var $hasMany = array('UserCourse' => array('className'   => 'UserCourse',
                                             'conditions'  => 'UserCourse.record_status = "A"',
                                             'order'       => 'UserCourse.course_id ASC',
                                             'limit'       => '99999',
                                             'foreignKey'  => 'user_id',
                                             'dependent'   => true,
                                             'exclusive'   => false,
                                             'finderSql'   => ''),
                       'UserEnrol' => array('className'   => 'UserEnrol',
                                            'conditions'  => 'UserEnrol.record_status = "A"',
                                            'order'       => 'UserEnrol.course_id ASC',
                                            'limit'       => '99999',
                                            'foreignKey'  => 'user_id',
                                            'dependent'   => true,
                                            'exclusive'   => false,
                                            'finderSql'   => '')
                      );*/

  var $hasAndBelongsToMany = array('Course' =>
                                   array('className'    => 'Course',
                                         'joinTable'    => 'user_courses',
                                         'foreignKey'   => 'user_id',
                                         'associationForeignKey'    =>  'course_id',
                                         'conditions'   => '',
                                         'order'        => '',
                                         'limit'        => '',
                                         'unique'       => true,
                                         'finderQuery'  => '',
                                         'deleteQuery'  => '',
                                         'dependent'    => true,
                                        ),
                                   'Enrolment' =>
                                   array('className'    => 'Course',
                                         'joinTable'    => 'user_enrols',
                                         'foreignKey'   => 'user_id',
                                         'associationForeignKey'    =>  'course_id',
                                         'conditions'   => '',
                                         'order'        => '',
                                         'limit'        => '',
                                         'unique'       => true,
                                         'finderQuery'  => '',
                                         'deleteQuery'  => '',
                                         'dependent'    => true,
                                        ),
                                   'Group' =>
                                   array('className'    => 'Group',
                                         'joinTable'    => 'groups_members',
                                         'foreignKey'   => 'user_id',
                                         'associationForeignKey'    =>  'group_id',
                                         'conditions'   => '',
                                         'order'        => '',
                                         'limit'        => '',
                                         'unique'       => true,
                                         'finderQuery'  => '',
                                         'deleteQuery'  => '',
                                         'dependent'    => true,
                                        ),
                                   'Role' =>
                                   array('className'    => 'Role',
                                         'joinTable'    => 'roles_users',
                                         'foreignKey'   => 'user_id',
                                         'associationForeignKey'    => 'role_id',
                                         'conditions'   => '',
                                         'order'        => '',
                                         'limit'        => '',
                                         'unique'       => true,
                                         'finderQuery'  => '',
                                         'deleteQuery'  => '',
                                         'dependent'    => true,
                                        )
                                   );

  var $validate = array('username'  => array('character' => array('rule' => 'alphaNumeric',
                                                                  'required' => true,
                                                                  'message' => 'Alphabets and numbers only'),
                                             'unique' => array('rule' => 'isUnique',
                                                               'messages' => 'Duplicate Username found. Please change the username.')),
                        'email'     => array('rule' => 'email',
                                             'required' => false,
                                             'allowEmpty' => true,
                                             'message' => 'Invalid email format'));

  function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);
    $this->virtualFields['full_name'] = sprintf('CONCAT(%s.first_name, " ", %s.last_name)', $this->alias, $this->alias);
    $this->virtualFields['student_no_with_full_name'] = sprintf('CONCAT(%s.student_no, " - ", %s.first_name, " ", %s.last_name)', $this->alias, $this->alias, $this->alias);
  }

  //Overwriting Function - will be called before save operation
  function beforeSave() {
    $allowSave = true;

    if(!isset($this->data[$this->name]['id']) && empty($this->data[$this->name]['password'])) {
      $this->data[$this->name]['password'] = <PASSWORD>::randomPassword(6);
    }

    // clear password to avoid updating to a empty one
    if(empty($this->data[$this->name]['password'])) {
      unset($this->data[$this->name]['password']);
    }

    return $allowSave && parent::beforeSave();
  }

  function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
    if(!isset($fields['contain'])) {
    } elseif ($fields['contain'] === false) {
      $fields['contain'] = array('Creator', 'Updater');
    } else {
      $fields['contain'] = array_merge($fields['contain'], array('Creator', 'Updater'));
    }
    return parent::find($conditions, $fields, $order, $recursive);
  }

  //Validation check on duplication of username
  function hasDuplicateUsername($username) {
    if ($this->find('first', array('conditions' => array('username' => $username)))) {
      $this->errorMessage='Duplicate Username found. Please change the username of this user.';
      /*if ($this->data[$this->name]['role'] == 'S') {
        $this->errorMessage.='<br>If you want to enrol this student to one or more courses, use the enrol function on User Listing page.';
        }*/
      return false;
    }

    return true;
{'fraction_non_alphanumeric': 0.13336056962764095, 'fraction_numerical': 0.003385082292517801, 'mean_word_length': 0.9693138719687392, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 233, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 276, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7177640', 'n_tokens_mistral': 3695, 'n_tokens_neox': 3559, 'n_words': 743}