Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User upload #31

Merged
merged 17 commits into from Aug 25, 2012
2 changes: 1 addition & 1 deletion application/config/config.php
Expand Up @@ -15,7 +15,7 @@
|
*/
$config['base_url'] = '';

$config['student_pic_upload_path'] = 'uploads/students/pictures/';
/*
|--------------------------------------------------------------------------
| Index File
Expand Down
60 changes: 56 additions & 4 deletions application/controllers/student.php
Expand Up @@ -5,7 +5,7 @@ class Student extends MY_Controller {

public function __construct(){
parent::__construct();

$this->load->helper("image_helper");
}

public function index()
Expand Down Expand Up @@ -70,6 +70,7 @@ public function edit_form(){

//Create list of schools view
$data["schools"] = $this->student_model->get_schools();
$data["upload_errors"] = '';

$this->load->view('student/edit_student_form', $data);
}
Expand Down Expand Up @@ -133,7 +134,7 @@ public function edit(){
$data["majors"] = $this->student_model->get_majors();
//Create list of schools view
$data["schools"] = $this->student_model->get_schools();

$data["upload_errors"] = '';
$this->load->view('student/edit_student_form', $data);

//Else, add student to database
Expand Down Expand Up @@ -180,14 +181,65 @@ public function edit(){
endif;

}


public function upload_profile_pic(){

//set the path to root
$config['upload_path'] = './uploads/students/pictures';
$config['allowed_types'] = 'gif|jpg|png';
//append unix time stamp for unique update
$config['file_name'] = "studentpic_" . $this->current_student_id . "_" . time();

//2000kb and max image width and height
$config['max_size'] = '2000';
$config['max_width'] = '1024';
$config['max_height'] = '768';

//prevent users from uploading multiple images.
$config['overwrite'] = TRUE;

//load the upload lib with settings
$this->load->library('upload', $config);
if (!$this->upload->do_upload()):
$data["student_logged_in"] = $this->current_student_info;
$data["majors"] = $this->student_model->get_majors();
$data["schools"] = $this->student_model->get_schools();
$data["upload_errors"] = $this->upload->display_errors();

$this->load->view('student/edit_student_form', $data);
else:
$uploaded_data = $this->upload->data();
$status = $this->student_model->update_profile_picture($this->current_student_id, $uploaded_data['file_name']);

$this->load->library('message');
if ($status):
$this->message->set("Picture updated successfully", "success", TRUE);
redirect("student/edit_form");
else:
$this->message->set("Picture update failed", "error", TRUE);
redirect("student/edit_form");
endif;
endif;

}

public function remove_profile_pic(){
$status = $this->student_model->delete_profile_picture($this->current_student_id);
$this->load->library('message');
if ($status):
$this->message->set("Picture deleted successfully", "success", TRUE);
redirect("student/edit_form");
else:
$this->message->set("Picture delete failed", "error", TRUE);
redirect("student/edit_form");
endif;
}

//View all students
public function view_all()
{
//Retrieve all students information to send to view
$data["students"] = $this->student_model->get_all_students();

$this->load->view('student/view_all_students', $data);
}

Expand Down
21 changes: 21 additions & 0 deletions application/helpers/image_helper.php
@@ -0,0 +1,21 @@
<?php

function student_picture_src($student_id, $oauth_uid, $picture){

$CI =& get_instance();
$CI->load->helper("path_helper");
$student_pic_url = student_picture_url();

if($picture):
return $student_pic_url . $picture;
else:
if($oauth_uid):
return "https://graph.facebook.com/". $oauth_uid . "/picture?type=large"; //high resolution
else:
return "http://i0.kym-cdn.com/photos/images/original/000/074/586/tumblr_l7l1nqA5va1qdsweso1_1280.jpg?1318992465"; //dummy image
endif;
endif;

}

//end of image_helper.php
23 changes: 23 additions & 0 deletions application/helpers/path_helper.php
@@ -0,0 +1,23 @@
<?php
// application/helpers/path_helper.php
if (!function_exists('asset_url'))
{
function asset_url()
{
// the helper function doesn't have access to $this, so we need to get a reference to the
// CodeIgniter instance. We'll store that reference as $CI and use it instead of $this
$CI =& get_instance();
// return the asset_url
return base_url() . $CI->config->item('asset_path');
}

function student_picture_url()
{
$CI =& get_instance();
// return the asset_url
return base_url() . $CI->config->item('student_pic_upload_path');
}

}

/*End of path_helper located in application/helpers*/
38 changes: 34 additions & 4 deletions application/models/student_model.php
Expand Up @@ -113,8 +113,6 @@ public function oauth_authenticate($oauth_id, $email="None", $first_name, $last_
if($rows > 0):
return $result;
else:
//echo "mufuka he ain't exist";exit(4);

$data = array(
'oauth_uid' => $oauth_id,
'first' => $first_name ,
Expand All @@ -132,9 +130,41 @@ public function oauth_authenticate($oauth_id, $email="None", $first_name, $last_
$rows = $query->num_rows();
$result = $query->row();
return $result;
endif;

endif;
endif;
}

public function update_profile_picture($student_id, $file_name){
//update student set picture = blah where id = blah
$data = array(
'picture' => $file_name,
);
$this->db->where('student_id', $student_id);
$this->db->update('students', $data);

$rows_affected = $this->db->affected_rows();

if($rows_affected > 0)
return TRUE;
else
return FALSE;

}

public function delete_profile_picture($student_id){
$data = array(
'picture' => NULL,
);

$this->db->where('student_id', $student_id);
$this->db->update('students', $data, FALSE);

$rows_affected = $this->db->affected_rows();

if($rows_affected > 0)
return TRUE;
else
return FALSE;
}

}
18 changes: 15 additions & 3 deletions application/views/student/edit_student_form.php
Expand Up @@ -22,7 +22,7 @@
endforeach;

//Settings for form elements

$first = array(
'name' => 'first',
'value' => set_value('first', $student_logged_in->first)
Expand Down Expand Up @@ -130,10 +130,22 @@
</hgroup>

<?php $this->message->display(); ?>

<!--Get the students profile picture source.. Will add helper for this -->
<?php $pic_src = student_picture_src($student_logged_in->student_id, $student_logged_in->oauth_uid, $student_logged_in->picture); ?>
<img src="<?php echo $pic_src; ?>" width="100px" height="100px"/>
<?php echo anchor("student/remove_profile_pic", "Remove Picture"); ?>
<!--begin upload form-->
<?php echo $upload_errors;?>
<?php echo form_open_multipart('student/upload_profile_pic');?>
<input type="file" name="userfile" size="20" />
<input type="submit" value="upload" />
</form>
<!--end upload form-->

<?php echo form_open('student/edit/' . $student_logged_in->student_id, array("id" => "edit-profile")); ?>

<?php echo validation_errors('<p class="error-message">', '</p>'); ?>

<h2>Personal Information</h2>
<?php echo form_label('First Name:', 'first'); echo form_input($first);?>
<?php echo form_label('Last Name:', 'last'); echo form_input($last); ?>
Expand Down
3 changes: 3 additions & 0 deletions application/views/student/student_block.php
@@ -1,7 +1,10 @@
<section class="listing">
<header>
<?php $pic_src = student_picture_src($student->student_id, $student->oauth_uid, $student->picture); ?>
<img src="<?php echo $pic_src; ?>" width="25px" height="25px"/>
<h2><?php echo $student->first . ' ' . $student->last; ?></h2>
<h3><?php echo $student->school_id . ' ' . $student->year; ?> - <?php echo $student->major_id ?>
<h3><?php if($student->status) echo $student->status; ?></h3>
<br />Arts & Sciences 2014 - Computer Science</h3>
</header>
<div class="float-right">
Expand Down