Exemple #1
0
 public static function attachmentDelete($post_id, $attach_id)
 {
     $id = DB::createQuery('post_likes')->fields('id')->where(array('attachment_id' => $attach_id, 'post_id' => $post_id))->select();
     for ($i = 0; $i < count($id); $i++) {
         $id = $this->createQuery()->delete($id[$i]['id']);
     }
     self::clearCache();
 }
Exemple #2
0
 /**
  * Add a comment to a post
  *
  * @param int $post_id	Id of the post
  * @param int $user_id	Id of the user
  * @param string $message	Content of the comment
  * @return int	Id of the new comment
  */
 public function add($post_id, $user_id, $message, $attachment_id = null)
 {
     if (isset($attachment_id)) {
         $attachment = DB::createQuery('attachments')->select($attachment_id);
         if (!isset($attachment[0])) {
             throw new Exception('Attachment not found!');
         }
     } else {
         $attachment_id = null;
     }
     $id = $this->createQuery()->set(array('post_id' => $post_id, 'user_id' => $user_id, 'message' => $message, 'time' => time(), 'attachment_id' => $attachment_id))->insert();
     Post_Model::clearCache();
     return $id;
 }
Exemple #3
0
 public function add($post_id, $user_id, $attachment_id = null)
 {
     if (isset($attachment_id)) {
         $attachment = DB::createQuery('attachments')->select($attachment_id);
         if (!isset($attachment[0])) {
             throw new Exception('Attachment not found!');
         }
     } else {
         $attachment_id = null;
     }
     $already = $this->createQuery()->where(array('post_id' => $post_id, 'user_id' => $user_id, 'attachment_id' => $attachment_id))->select();
     if (!empty($already)) {
         throw new Exception('Already Liked !');
     }
     $id = $this->createQuery()->set(array('post_id' => $post_id, 'user_id' => $user_id, 'attachment_id' => $attachment_id))->insert();
     self::clearCache();
     return $id;
 }
Exemple #4
0
 /**
  * Add a vote in a survey
  *
  * @param int $id	Id of the survey
  * @param array $id	Ids of the answers
  * @param string $username	User name
  * @return int	Id of the corresponding post
  */
 public function vote($id, $votes, $username)
 {
     if (count($votes) == 0) {
         throw new Exception('You should give at least one answer');
     }
     $surveys = $this->createQuery()->select($id);
     if (!isset($surveys[0])) {
         throw new Exception('Survey not found');
     }
     $survey = $surveys[0];
     unset($surveys);
     if ($survey['multiple'] != '1' && count($votes) != 1) {
         throw new Exception('You must choose exactly one answer');
     }
     if (strtotime($survey['date_end']) < time()) {
         throw new Exception('The survey is closed');
     }
     $post_model = new Post_Model();
     $post = $post_model->getRawPost((int) $survey['post_id']);
     $answers = DB::createQuery('survey_answers')->fields('id', 'votes')->where(array('survey_id' => $survey['id']))->select();
     foreach ($answers as $answer) {
         $answer['votes'] = $answer['votes'] == '' ? array() : json_decode($answer['votes'], true);
         if (in_array($username, $answer['votes']) && !in_array((int) $answer['id'], $votes)) {
             array_splice($answer['votes'], array_search($username, $answer['votes']), 1);
             $weight = -1;
         } else {
             if (!in_array($username, $answer['votes']) && in_array((int) $answer['id'], $votes)) {
                 $answer['votes'][] = $username;
                 $weight = 1;
             } else {
                 continue;
             }
         }
         DB::createQuery('survey_answers')->set(array('votes' => json_encode($answer['votes']), 'nb_votes = nb_votes' . ($weight == 1 ? '+1' : '-1')))->update((int) $answer['id']);
     }
     Post_Model::clearCache();
     return (int) $post['id'];
 }
Exemple #5
0
        try {
            if (!isset($students[0])) {
                throw new Exception('Student n°' . $student_number . ' (' . $firstname . ' ' . $lastname . ') not found!');
            }
            $student = $students[0];
            //if($student['firstname'] != $firstname && $student['lastname'] != $lastname)
            //	throw new Exception('Student n°'.$student_number.' : different names : '.$firstname.' '.$lastname.' != '.$student['firstname'].' '.$student['lastname']);
            if ($promo == 'CESURE-A2-A3') {
                DB::createQuery('students')->set('cesure', 1)->where(array('student_number' => $student_number))->update();
            }
            $users = DB::select('
				SELECT 1
				FROM users
				WHERE username = '******'username']) . '
			');
            $db_query = DB::createQuery('users')->set(array('address' => $address, 'zipcode' => $zipcode, 'city' => $city, 'cellphone' => $cellphone, 'phone' => $phone));
            if (isset($birthday)) {
                $db_query->set('birthday', $birthday);
            }
            if (!isset($users[0])) {
                echo 'Creating user "' . $student['username'] . '"' . "\n";
                $db_query->set(array('username' => $student['username']))->insert();
            } else {
                $db_query->where(array('username' => $student['username']))->update();
            }
        } catch (Exception $e) {
            echo $e->getMessage() . "\n";
        }
    }
    /*
    $users = DB::select('
Exemple #6
0
 public function addadmin($username)
 {
     if ($this->checkuser($username, 1)) {
         DB::createQuery('users')->set(array('admin' => 1))->where(array('username' => $username))->update();
     }
 }
Exemple #7
0
 /**
  * Delete a post
  *
  * @param int $post_id		Post's id
  */
 public function delete($post_id)
 {
     // Delete attachments
     $attachments = DB::createQuery('attachments')->fields('id', 'ext')->where(array('post_id' => $post_id))->select();
     foreach ($attachments as $attachment) {
         File::delete(self::getAttachedFilePath((int) $attachment['id'], $attachment['ext']));
         File::delete(self::getAttachedFilePath((int) $attachment['id'], 'jpg', 'thumb'));
     }
     // Delete the post
     $this->createQuery()->delete($post_id);
     self::clearCache();
     // Delete from the search index
     $search_model = new Search_Model();
     $search_model->delete('post', $post_id);
 }
Exemple #8
0
 /**
  * Delete only one attachment
  *
  * @param int $post_id		Attachment's id
  */
 public function deleteattachment($id, $post_id)
 {
     // Delete attachments
     $attachment = DB::createQuery('attachments')->fields('id', 'ext')->where(array('id' => $id, 'post_id' => $post_id))->select();
     if (count($attachment[0]) > 0) {
         File::delete(self::getAttachedFilePath((int) $attachment[0]['id'], $attachment[0]['ext']));
         File::delete(self::getAttachedFilePath((int) $attachment[0]['id'], 'jpg', 'thumb'));
         // Delete the attachment
         DB::createQuery('attachments')->where(array('id' => $id, 'post_id' => $post_id))->delete();
         PostComment_Model::attachmentDelete($id, $post_id);
         PostCommentLike_Model::attachmentDelete($id, $post_id);
         PostLike_Model::attachmentDelete($id, $post_id);
         self::clearCache();
         return true;
     } else {
         return false;
     }
 }
Exemple #9
0
 /**
  * Save the data of a student
  *
  * @param string $username	student's username
  * @param array $data	student's data
  */
 public function save($username, $data)
 {
     $student_data = array();
     $old_data = DB::createQuery('students')->fields('firstname', 'lastname', 'student_number')->where(array('username' => $username))->select();
     if (!$old_data[0]) {
         throw new Exception('Student not found');
     }
     $old_data = $old_data[0];
     $change_name = false;
     // Firstname
     if (isset($data['firstname']) && $old_data['firstname'] != trim($data['firstname'])) {
         if (trim($data['firstname']) == '') {
             throw new FormException('firstname');
         }
         $student_data['firstname'] = trim($data['firstname']);
         $change_name = true;
     }
     // Lastname
     if (isset($data['lastname']) && $old_data['lastname'] != trim($data['lastname'])) {
         if (trim($data['lastname']) == '') {
             throw new FormException('lastname');
         }
         $student_data['lastname'] = trim($data['lastname']);
         $change_name = true;
     }
     // Student number
     if (isset($data['student_number'])) {
         if (!ctype_digit(trim($data['student_number']))) {
             throw new FormException('student_number');
         }
         $student_data['student_number'] = (int) trim($data['student_number']);
         // Moving the avatar
         if ($student_data['student_number'] != $old_data['student_number']) {
             // Thumb
             $avatar_path = self::getAvatarPath($student_data['student_number'], true);
             $avatar_dir = File::getPath($avatar_path);
             if (!is_dir($avatar_dir)) {
                 File::makeDir($avatar_dir, 0777, true);
             }
             File::rename(self::getAvatarPath($old_data['student_number'], true), $avatar_path);
             // Big
             $avatar_path = self::getAvatarPath($student_data['student_number'], false);
             $avatar_dir = File::getPath($avatar_path);
             if (!is_dir($avatar_dir)) {
                 File::makeDir($avatar_dir, 0777, true);
             }
             File::rename(self::getAvatarPath($old_data['student_number'], false), $avatar_path);
         }
     }
     // Promo
     if (isset($data['promo'])) {
         if (!ctype_digit(trim($data['promo'])) || (int) $data['promo'] < 2000) {
             throw new FormException('promo');
         }
         $student_data['promo'] = (int) trim($data['promo']);
     }
     // Cesure
     if (isset($data['cesure'])) {
         $student_data['cesure'] = $data['cesure'] ? 1 : 0;
     }
     // Avatar
     if (isset($data['avatar_path']) && isset($data['student_number']) && File::exists($data['avatar_path'])) {
         $avatar_path = self::getAvatarPath((int) $data['student_number'], true);
         $avatar_dir = File::getPath($avatar_path);
         if (!is_dir($avatar_dir)) {
             File::makeDir($avatar_dir, 0777, true);
         }
         File::rename($data['avatar_path'], $avatar_path);
     }
     if (isset($data['avatar_big_path']) && isset($data['student_number']) && File::exists($data['avatar_big_path'])) {
         $avatar_path = self::getAvatarPath((int) $data['student_number'], false);
         $avatar_dir = File::getPath($avatar_path);
         if (!is_dir($avatar_dir)) {
             File::makeDir($avatar_dir, 0777, true);
         }
         File::rename($data['avatar_big_path'], $avatar_path);
     }
     // Update the DB
     $this->createQuery()->set($student_data)->where(array('username' => $username))->update();
     if ($change_name) {
         Post_Model::clearCache();
         // Update the search index
         $search_model = new Search_Model();
         $search_model->index(array('username' => $username, 'firstname' => Search_Model::sanitize(isset($student_data['firstname']) ? $student_data['firstname'] : $old_data['firstname']), 'lastname' => Search_Model::sanitize(isset($student_data['lastname']) ? $student_data['lastname'] : $old_data['lastname'])), 'student', $username);
     }
 }
Exemple #10
0
 * Fill the directory with the info of the LDAP
 *
 * @example /usr/bin/php -f avatars.php
 */
define('CLI_MODE', true);
define('APP_DIR', realpath(dirname(__FILE__) . '/../') . '/');
define('CF_DIR', realpath(dirname(__FILE__) . '/../../confeature/') . '/');
define('DATA_DIR', realpath(dirname(__FILE__) . '/../../data/') . '/');
try {
    // Loading Confeature
    require_once CF_DIR . 'init.php';
    $avatars_tmp_path = DATA_DIR . Config::DIR_DATA_TMP . '/avatars';
    if (!is_dir($avatars_tmp_path)) {
        throw new Exception($avatars_tmp_path . ' not found!');
    }
    $students = DB::createQuery('students')->fields('student_number', 'firstname', 'lastname')->select();
    foreach ($students as $student) {
        try {
            $avatar_path = Student_Model::getAvatarPath((int) $student['student_number']);
            $avatar_thumb_path = Student_Model::getAvatarPath((int) $student['student_number'], true);
            if (file_exists($avatar_path)) {
                continue;
            }
            $original_path = $avatars_tmp_path . '/' . $student['student_number'] . '.jpg';
            if (!file_exists($original_path)) {
                throw new Exception('Photo of the student n°' . $student['student_number'] . ' (' . $student['firstname'] . ' ' . $student['lastname'] . ') not found!');
            }
            $avatar_dir = File::getPath($avatar_path);
            if (!is_dir($avatar_dir)) {
                File::makeDir($avatar_dir, 0777, true);
            }
Exemple #11
0
    $students = array();
    foreach ($results as $result) {
        if (!isset($result['employeenumber'][0]) || !ctype_digit($result['employeenumber'][0]) || $result['employeenumber'][0] == '0' || !ctype_digit($result['title'][0]) || $result['title'][0] < 2000) {
            continue;
        }
        // If it's a test account (ISEP admin...), we continue
        if ((int) $result['employeenumber'][0] > 8000) {
            continue;
        }
        $lastname = ucwords(str_replace('-', ' ', $result['sn'][0]));
        $lastname = preg_replace('#(?<=^| )De #', 'de ', $lastname);
        $firstname = str_replace(' ', '-', ucwords(str_replace('-', ' ', $result['givenname'][0])));
        $students[] = array('username' => $result['uid'][0], 'firstname' => $firstname, 'lastname' => $lastname, 'student_number' => (int) $result['employeenumber'][0], 'promo' => (int) $result['title'][0]);
    }
    // Empty the students table
    DB::createQuery('students')->force()->delete();
    // Fill the students table
    while (count($students) != 0) {
        $students_ = array_splice($students, 0, 100);
        foreach ($students_ as $i => $student) {
            foreach ($student as $j => $field) {
                $student[$j] = DB::quote($field);
            }
            $students_[$i] = implode(',', $student);
        }
        DB::execute('
			INSERT INTO students
			(username, firstname, lastname, student_number, promo)
			VALUES
			(' . implode('),(', $students_) . ')
		');
Exemple #12
0
 /**
  * Creates a new instance of DB_Query with the table name corresponding to the model name
  *
  * @return DB_Query	New instance
  */
 protected function createQuery()
 {
     return DB::createQuery($this->table);
 }
Exemple #13
0
 * @example /usr/bin/php -f searchindex.php
 */
define('CLI_MODE', true);
define('APP_DIR', realpath(dirname(__FILE__) . '/../') . '/');
define('CF_DIR', realpath(dirname(__FILE__) . '/../../confeature/') . '/');
define('DATA_DIR', realpath(dirname(__FILE__) . '/../../data/') . '/');
try {
    // Loading Confeature
    require_once CF_DIR . 'init.php';
    $search_model = new Search_Model();
    // Students indexing
    $search_model->delete('student');
    $students = DB::createQuery('students')->fields('username', 'firstname', 'lastname')->select();
    foreach ($students as $student) {
        $search_model->index(array('username' => $student['username'], 'firstname' => Search_Model::sanitize($student['firstname']), 'lastname' => Search_Model::sanitize($student['lastname'])), 'student', $student['username']);
    }
    // Posts indexing
    $search_model->delete('post');
    $posts = DB::createQuery('posts')->fields('id', 'message', 'private', 'official')->select();
    foreach ($posts as $post) {
        $search_model->index(array('message' => Search_Model::sanitize($post['message']), 'official' => $post['official'] == '1', 'private' => $post['private'] == '1'), 'post', $post['id']);
    }
    // Groups indexing
    $search_model->delete('group');
    $groups = DB::createQuery('groups')->fields('id', 'name', 'url_name', 'description')->select();
    foreach ($groups as $group) {
        $search_model->index(array('name' => Search_Model::sanitize($group['name']), 'url_name' => $group['url_name'], 'description' => Search_Model::sanitize($group['description'])), 'group', $group['id']);
    }
} catch (Exception $e) {
    echo $e->getMessage();
}
Exemple #14
0
 public function insertUsers($username, $admin, $mail, $msn, $jabber, $address, $zipcode, $city, $cellphone, $phone, $birthday)
 {
     DB::createQuery('students')->set(array('username' => $username, 'admin' => $admin, 'mail' => $mail, 'msn' => $msn, 'jabber' => $jabber, 'address' => $address, 'zipcode' => $zipcode, 'city' => $city, 'cellphone' => $cellphone, 'phone' => $phone, 'birthday' => $birthday))->insert();
 }
Exemple #15
0
 /**
  * Returns true if a group already exists with this url_name, false otherwise
  *
  * @return boolean
  */
 public static function urlExists($url_name)
 {
     $result = DB::createQuery('groups')->fields('1')->where(array('url_name' => $url_name))->select();
     return isset($result[0]);
 }