File:  [LON-CAPA] / loncom / interface / loncourseuser.pm
Revision 1.1: download - view: text, annotated - select for diffs
Sun Apr 2 03:16:27 2023 UTC (12 months, 3 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_4_msu, HEAD
- Bug 6979
  - Shortcut to student's view of problem etc. from 'View As' item in Functions
  menu above problem.
  - Part of code in &submission() subroutine in grades.pm moved to separate
  subroutine -- &show_last_submission() which can be called by &view_as_user(),
  which in turn is called from lonhomework.pm

# The LearningOnline Network
# Verify user is in course
#
# $Id: loncourseuser.pm,v 1.1 2023/04/02 03:16:27 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#

package Apache::loncourseuser;

use strict;
use Apache::Constants qw(:common :http);
use Apache::lonnet;
use Apache::loncommon;
use Apache::lonuserutils;
use JSON::DWIW;
use LONCAPA qw(:DEFAULT :match);

sub handler {
    my $r = shift;
    &Apache::loncommon::content_type($r,'application/json');
    $r->send_http_header;
    my @userinfo;
    if ($env{'request.course.id'}) {
        my $canview;
        foreach my $priv ('mgr','vgr') {
            $canview = &Apache::lonnet::allowed($priv,$env{'request.course.id'});
            if (!$canview && $env{'request.course.sec'} ne '') {
                if (&Apache::lonnet::allowed($priv,"$env{'request.course.id'}/$env{'request.course.sec'}")) {
                    $canview = 'section';
                }
            }
            last if ($canview);
        }
        if ($canview) {
            my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
            my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
            my $crstype = &Apache::loncommon::course_type();
            if (($cdom) && ($cnum)) {
                my $possudom = $env{'form.udom'};
                if (&Apache::lonnet::domain($possudom) ne '') { 
                    my $possuname = $env{'form.uname'};
                    my $possuid = $env{'form.uid'};
                    if (($possuname eq '') && ($possuid ne '')) {
                        $possuname=(&Apache::lonnet::idget($possudom,[$possuid],'ids'))[1];
                    }
                    if ($possuname ne '') { 
                        if (&is_course_user($possudom,$possuname,$cdom,$cnum,$canview,
                                            $crstype,$env{'request.course.sec'})) {
                            @userinfo = ($possuname,$possudom);
                        }
                    }
                }
            }
        }
    }
    $r->print(JSON::DWIW->to_json({match => \@userinfo}));
    return OK;
}

sub is_course_user {
    my ($possudom,$possuname,$cdom,$cnum,$canview,$crstype,$section) = @_;
    my $found;
    my @types = ('active','future','previous');
    my @roles = &Apache::lonuserutils::roles_by_context('course',1,$crstype);
    my %userhash = &Apache::lonnet::get_my_roles($possuname,$possudom,'userroles',\@types,\@roles,[$cdom],1);
    if (keys(%userhash)) {
        foreach my $key (keys(%userhash)) {
            next unless ($key =~ /^\Q$cnum:$cdom:\E/);
            if ($canview eq 'section') {
                my $usec = (split(/:/,$key))[-1];
                if ($usec eq $section) {
                   $found = 1;
                }
            } else {
                $found = 1;
            }
            last if ($found);
        }
    }
    return $found;
}

1;


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>