#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "../global.h"
#include "../custom.h"

#include "shared_util.h"
#include "shared_access.h"
#include "shared_person_list.h"
#include "shared_central_user.h"
#include "shared_file_util.h"
#include "shared_authenticate.h"
#include "shared_bio.h"
#include "shared_cs_util.h"

/** Should the user, represented by 'user', be allowed to edit the bio
*** of target_username?
***
***  'course' represents the internal name subdir/dir of the course they are
***  working from. 'conf' in the configuration data for that course.
***
*** If the course string has zero length, the person is coming from the My Manhattan
*** page, or possibly from the administration system.  In this case the conf structure
*** contains garbage and can't be used.
**/
int
bio_edit_allowed(SESSION *user, const char *target_username, 
                                                      const char *course, CONFIG_STRUCT *conf)
{

 int from_within_course;
 int user_viewing_own;
 
 if(user->group == ADMIN)   /* administrators can always edit anyone's profile */
     return 1;
 
 
 from_within_course = strlen(course);   /* is user calling this from within a course? */
 user_viewing_own = !strcmp(user->username, target_username);  /* is user viewing his own profile? */

 /* if the user is working from within a course, allow them to edit their profile
 **  -- if the profile is their own and
 **  -- if the People module is enabled for that course and
 **  -- if the course has not been marked as read-only by the system admin
 */
 if(from_within_course)
    return user_viewing_own  
                    && has_write_permission(user,conf);   /* shared_access.c */
                    
  /** still here?
  *** The user is not an administrator and is not working from within a course.
  *** They are coming from the mymanhattan program.  They must be working with
  *** their own bio, an ENABLE_VIEW_EDIT_BIO_BUTTON in custom.h must be set
  *** at the time manhattan was compiled
  **/                 
    
 return user_viewing_own && ENABLE_VIEW_EDIT_BIO_BUTTON;   
           
} 



int
bio_view_allowed(SESSION *user, const char *target_username, 
                                                      const char *course, CONFIG_STRUCT *conf)
{
 int from_within_course;
 
  /* if they are allowed to edit a bio, then they can also view the bio */
  if(bio_edit_allowed(user, target_username, course, conf))
      return 1;
      
      
  /* Still here? Remaining handles users who did not pass the bio_edit_allowed() test **/
       
 from_within_course = strlen(course);
 /* users who did not pass the can not view other profiles unless they are coming from within a course */
 if(!from_within_course)
     return 0;     
     
 if(!are_classmates(user->username, target_username, conf))  /* shared_person_list.c */
      return 0;    
 
 
 if((user->group == STUDENT) && (!conf->people))  /*students can not view profiles unless the People module is enabled */
          return 0;
          
 return 1;
 
} 


/** Unfortunately, it's possible for a person to have more than one real name in Manhattan
*** There's the 'central' Name and a name stored in the password file for each course.
*** Most times they are the same, but a teacher can change a person's name within a course.
*** This function returns in target_realname, either the central realname or the 
*** realname stored within a particular course, depending on whether the 'course' string
*** is of zero length
**/


void 
get_bio_realname(const char *course, CONFIG_STRUCT *conf, const char *target_username, char *target_realname)
{
 int from_within_course;
 SESSION target_user;
 
 from_within_course = strlen(course);
 
 if(!from_within_course)  /* then realname is from central store */
    get_central_user_info(target_username, &target_user, 0);     /* shared_central_user.c */
 else  /* get realname from the password file for this course */
    get_user_info(target_username, &target_user, conf);     /* shared_person_list.c */
    
  strncpy(target_realname, target_user.realname, MAX_NAME + 1);
}



void  
get_bio_dir(const char *course, CONFIG_STRUCT *conf, const char *target_username, char *bio_dir)
{
 if(*course && conf->standalone)   /* if we're coming from a standalone course */
   snprintf(bio_dir, MAX_PATH + 1, "%s%s/%s/%s",
       conf->course_path,
       PEOPLE_DIR,
       target_username,
       BIO_DIR);
 else                           /* profile is in central users directory */
      snprintf(bio_dir, MAX_PATH + 1, "../%s/%c/%s/%s", 
              USERS_DIR, 
              sub_dir_name(target_username),     /* shared_authenticate.c */
              target_username,
              BIO_DIR);

}      


int
bio_photo_exists(const char *bio_dir)
{
 char full_path[MAX_PATH + 1];
 
 snprintf(full_path,MAX_PATH + 1, "%s/%s", bio_dir, BIO_PIC_FNAME);
 return file_exists(full_path);  /* shared_file_util.c */
}




/** A bio exists if the text file (BIO_FNAME) or the
*** image file (BIO_PIC_FNAME) exists within bio_dir
*** call get_bio_dir() first (above) to get bio_dir
***/

int bio_exists(const char *bio_dir)
{
 char full_path[MAX_PATH + 1];
 
 snprintf(full_path,MAX_PATH + 1, "%s/%s", bio_dir, BIO_FNAME);
 if(file_exists(full_path))  /* shared_file_util.c */
    return 1;

 return bio_photo_exists(bio_dir);
}




void
cs_set_bio_link( const char *target_username, const char *course, 
		CONFIG_STRUCT *conf, const char *key, int grp, 
		int num, const char *prefix, int name_len)
{
  char bio_dir[MAX_PATH + 1];
  char name[name_len];
  char url[MAX_PATH + 1];
   
  snprintf(name, name_len, "%s.%d.url", prefix, num); 
  if(*course && conf->people && (grp != ANONYMOUS_DISCUSSION))
   {
    get_bio_dir(course, conf, target_username, bio_dir);
    if(bio_exists(bio_dir))
     {
       snprintf(url, MAX_PATH +1, "%s?id=%s&amp;user=%s&amp;crs=%s", "bio_view", key,target_username,course);
       cs_set_value(name, url);
     }
   }
}






