예제 #1
0
파일: functionLib.php 프로젝트: knichel/AIT
/**
 * This function will create a user based on array of info
 *
 * @global resource $db
 * @param int $user
 * @param string $where_clause
 * @param int $center_id
 * @return array (error, message)
 */
function createUser($user, $where_clause, $centers, $isActive = 'Y')
{
    global $db;
    $ret = array('user_id' => '', 'error' => false, 'message' => '');
    // remove potential white space...
    foreach ($user as &$val) {
        $val = makeSQLsafe(trim($val));
    }
    //$STGUID = $user['STGUID'];
    $error = false;
    $sql = "INSERT into users (" . implode(',', array_keys($user)) . ",parentLinkCode) VALUES('" . implode("','", $user) . "',UUID())\r\n                    ON DUPLICATE KEY UPDATE ";
    foreach ($user as $k => $v) {
        $sql .= "" . $k . " = '" . $v . "',";
    }
    $sql1 = trim($sql, ',');
    //print $sql1."<br>";
    /**
     * The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.
     * With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row and 2 if an existing row is updated.
     */
    $result1 = $db->query($sql1);
    //if(0 == $db->errno){
    $numAffectedRows = $db->affected_rows;
    //print "The number of affected rows for {".$sql1."} is ".$numAffectedRows."\n\n";
    if ($numAffectedRows == 1) {
        /** An Insert Happened */
        $result2 = $db->query("SELECT LAST_INSERT_ID() as userID");
        $row2 = $result2->fetch_assoc();
        $uid = $ret['user_id'] = $row2['userID'];
        foreach ($centers as $center_id) {
            $sql3 = "INSERT into user2center (user_id, center_id," . $where_clause . ", isActive) VALUES(" . $row2['userID'] . "," . $center_id . ",'Y','" . $isActive . "')";
            $result3 = $db->query($sql3);
            //print $sql3."<br>";
            if (0 != $db->errno) {
                $ret['error'] = true;
                $ret['message'] .= "The user was created, but there was an error adding the user {" . $sql3 . "} to the correct Building." . $db->error . "<br>\n";
            } else {
                if ($where_clause == 'isStudent') {
                    $msg = reActivate_student($uid, $center_id);
                    if ($msg['error']) {
                        $ret['message'] .= $msg['message'];
                    }
                }
                if ($where_clause == 'isTeacher') {
                    $msg = reActivate_teacher($uid, $center_id);
                    if ($msg['error']) {
                        $ret['message'] .= $msg['message'];
                    }
                }
            }
        }
    } elseif ($numAffectedRows == 2) {
        /** An Update Happened */
        // update took place no meesage to send...
        // Need to update user2center isActive status based on $user values...
        $sql2 = "SELECT user_id as userID FROM users where STGUID = '" . $STGUID . "'";
        $result2 = $db->query($sql2);
        $row2 = $result2->fetch_assoc();
        $uid = $row2['userID'];
        foreach ($centers as $CID) {
            $sql3 = "INSERT into user2center (user_id, center_id, isActive, isStudent)\r\n                    VALUES (" . $uid . "," . $CID . ",'" . $isActive . "','Y')\r\n                ON DUPLICATE KEY UPDATE isActive='" . $isActive . "'";
            $result3 = $db->query($sql3);
        }
    } else {
        /**  No information was Updated and No INSERT happened. */
        //print "The number of affected rows for {".$sql1."} is ".$numAffectedRows."\n\n";
        //$ret['error'] = true;
        //$ret['message'] .= "There was an error creating the user with {".$sql1."}.".$db->error."<br>";
        //$ret['query'] = $sql1;
    }
    return $ret;
}
예제 #2
0
     $send_progress = 'Y';
     $center_id = $_SESSION[$_CONF['sess_name'] . '_myCenter'];
 }
 $user = array("first_name" => $_POST['fname'], "last_name" => $_POST['lname'], "email" => $_POST['e_mail'], "u_name" => $_POST['u_name'], "passwd" => md5($u_passwd), "grade_level" => $grade_level, "school_id" => $school, "send_attend_email" => $send_attend, "weekly_progress" => $send_progress, "created_by" => $_SESSION[$_CONF['sess_name'] . '_myUID']);
 $userStatus = createUser($user, $where_clause, array($center_id));
 //exit;
 if (!$userStatus['error']) {
     $pid = $userStatus['user_id'];
     /** user was added to db, now send email... **/
     $b = "<br />";
     if ($usr_lev == 3) {
         /** now create the teacher folder in teacher_pages & downloads & dropbox & backups**/
         reActivate_teacher($pid, $center_id);
     }
     if ($usr_lev == 1) {
         reActivate_student($pid, $center_id);
         //$path = $configPath."DropBox/".$center_short_name."/students/";
         //mkdir(escapeshellarg($path).$_POST['u_name'], 0770);
         // chown user to be student username
         //chown($path.$_POST['u_name'],$_POST['u_name']);
     }
     $email_results = email_user_login_info($_POST['fname'], $_POST['lname'], $_POST['e_mail'], $_POST['u_name'], $u_passwd, $usr_lev);
     if ($email_results['error_value'] == true) {
         $errors = true;
         $b .= "The user account was created, however, the email did not get sent.  The mail server returned the following error, <br /><br />";
         $b .= $email_results['error_mesg'];
         $b .= "<br />Contact the Site Administrator {" . $_CONF['admin_email'] . "}";
         $b .= "\n\t\t\t\t\t\t\t<form action=index.php?lev=" . $_SESSION[$_CONF['sess_name'] . '_lev'] . "&cat=" . $_SESSION[$_CONF['sess_name'] . '_cat'] . " method=POST>\n\t\t\t\t\t\t\t<input type=submit class=submit name=link value=\"Continue\">\n\t\t\t\t\t\t\t</form>\n\t\t\t\t\t\t\t</center>";
         $color = "red";
     } else {
         $b .= "\n\t\t\t\t\t\t\t<center><form action=index.php?lev=" . $_SESSION[$_CONF['sess_name'] . '_lev'] . "&cat=" . $_SESSION[$_CONF['sess_name'] . '_cat'] . " method=POST>\n\t\t\t\t\t\t\t<input type=submit class=submit name=finish value=Continue></form></center>";