# ----------------------------------------------------------------------------- # tpb12 / admin / user_maintenance.pl # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # date description # ---------- ----------------------------------------------------------------- # 02/02/2012 use randomPassword instead of Crypt::GeneratePassword::word # 06/22/2012 stop using ITO::Config # 09/07/2012 last_update now int fk # 02/18/2013 replace $r->no_cache with set_cache_control (q.v. for details) # 03/11/2013 use standard libraries; remove change password # 09/05/2014 rename libraries to TPB12; rename program directory to tpb12 # ----------------------------------------------------------------------------- use strict; use warnings; use Apache2::Const -compile => qw(OK); use Apache2::Request; use Apache2::RequestIO; use Apache2::RequestUtil; use Date::Manip qw( UnixDate ); use Lingua::EN::NameCase qw( nc ) ; use Readonly; use ITO::Utility::FieldValidation; use ITO::Utility::LogError; use ITO::Utility::Utility; use ITO::TPB12::Utility::Emailer; use ITO::TPB12::Utility::Utility2; use ITO::TPB12::Config; use ITO::TPB12::RoleToUser; use ITO::TPB12::TechPlanToUser; use ITO::TPB::User; # ----------------------------------------------------------------------------- my $r = Apache2::RequestUtil->request; set_cache_control($r); my $req = Apache2::Request->new($r); $r->content_type('text/html; charset=utf-8'); Readonly my $TEMPLATE_PATH => getTemplatePaths($r); my ($error, $user) = userValidationTPB( $r, $TEMPLATE_PATH, 'tpb12 admin user_maintenance'); return $error if (defined $error); { no strict 'refs'; my $run_mode = $req->param('run_mode') || 'change_password'; my $subroutine = '_' . $run_mode; &{$subroutine}( $r, $req, $TEMPLATE_PATH, $user ); } # ----------------------------------------------------------------------------- # # request handlers # # ----------------------------------------------------------------------------- sub _do_user_plans { my ( $r, $req, $TEMPLATE_PATH, $user ) = @_; my %record = (); if (! $req->param('id')) { return errorToLogAndUser($r, $TEMPLATE_PATH, "Required field [id] is missing. (user_maintenance do_user_plans)", "There was an error. Please contact the system administrator.", ); } my $target_user = $user->retrieve( 'id' => $req->param('id') ); if ( ! defined $target_user ) { return errorToLogAndUser($r, $TEMPLATE_PATH, "User record not found. (user_maintenance do_user_plans)", "There was an error. Please contact the system administrator.", ); } $record{'target_user'} = $target_user; my $filtered = 0; my %where = ( 'user' => $target_user->id, ); my $search_type = $req->param('search_type'); if ($search_type) { ++ $filtered; $where{'tech_plan.plan_type'} = $search_type; $record{'search_type'} = $search_type; } my $search_purpose = $req->param('search_purpose'); if ($search_purpose) { ++ $filtered; $where{'tech_plan.purpose'} = $search_purpose; $record{'search_purpose'} = $search_purpose; $record{'search_purpose_text'} = ITO::TPB12::Purpose->retrieve($search_purpose)->name; } my $search_year = $req->param('search_year'); if ($search_year) { ++ $filtered; $where{'tech_plan.section_1_beginning'} = $search_year; $record{'search_year'} = $search_year; } my $search_created = $req->param('search_created'); if ($search_created) { ++ $filtered; $where{'tech_plan.date_created'} = { '-like' => $search_created . '%'}; $record{'search_created'} = $search_created; } my $search_status = $req->param('search_status'); if ($search_status) { ++ $filtered; $record{'search_status'} = $search_status; $search_status = 'Initiated' if ($search_status eq 'Not Started'); $where{'tech_plan.status'} = $search_status; } $record{'filtered'} = $filtered; my @tpu = ITO::TPB12::TechPlanToUser->deep_search_where(%where); my @tech_plans = map {$_->tech_plan} @tpu; my %tpb_hash = (); @{$record{'plans'}} = sort { _tech_plan_compare(\%tpb_hash, $a, $b) } @tech_plans; @{ $record{'purposes'} } = ITO::TPB12::Purpose->search_where( { 'status' => 'Active', }, { 'order_by' => 'seq' } ); $user->template_configure( -template_options => { INCLUDE_PATH => $TEMPLATE_PATH, } ); $user->template_define( 'template', 'user_plans.tt' ); $r->print( $user->template_render( 'template', %record ) ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- sub _list_users { my ( $r, $req, $TEMPLATE_PATH, $user ) = @_; my %record = (); $user->template_configure( -template_options => { INCLUDE_PATH => $TEMPLATE_PATH, } ); $user->template_define( 'list', 'list_users.tt' ); $r->print( $user->template_render( 'list', %record ) ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- sub _do_list_users { my ( $r, $req, $TEMPLATE_PATH, $user ) = @_; my %record = (); my $search_text = trim($req->param('search_text')); if ($search_text) { $record{'search_text'} = $search_text; my $pattern = "${search_text}%"; @{ $record{'users'} } = $user->search_where( [ { 'user' => { '-like' => $pattern }, }, { 'email' => { '-like' => $pattern }, }, { 'last_name' => { '-like' => $pattern }, }, ], { 'order_by' => 'last_name, first_name, user', }, ); } else { @{ $record{'users'} } = $user->retrieve_all_sorted_by('last_name, first_name, user'); } $user->template_configure( -template_options => { INCLUDE_PATH => $TEMPLATE_PATH, } ); $user->template_define( 'list', 'list_users.tt' ); $r->print( $user->template_render( 'list', %record ) ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- # # utility subroutines # # ----------------------------------------------------------------------------- sub _tech_plan_compare { my ($hash, $tech_plan_1, $tech_plan_2) = @_; my $id_1 = $tech_plan_1->id; my $id_2 = $tech_plan_2->id; if (! defined $$hash{$id_1}) { $$hash{$id_1}{district} = $tech_plan_1->district ? $tech_plan_1->district->name : ''; $$hash{$id_1}{school} = $tech_plan_1->school ? $tech_plan_1->school->name : ''; $$hash{$id_1}{title} = lc $tech_plan_1->title; } if (! defined $$hash{$id_2}) { $$hash{$id_2}{district} = $tech_plan_2->district ? $tech_plan_2->district->name : ''; $$hash{$id_2}{school} = $tech_plan_2->school ? $tech_plan_2->school->name : ''; $$hash{$id_2}{title} = lc $tech_plan_2->title; } return ( $$hash{$id_1}{district} cmp $$hash{$id_2}{district} || $$hash{$id_1}{school} cmp $$hash{$id_2}{school} || $$hash{$id_1}{title} cmp $$hash{$id_2}{title} ); } # -----------------------------------------------------------------------------