Diff for /loncom/homework/lonhomework.pm between versions 1.344.2.6 and 1.344.2.10.4.2

version 1.344.2.6, 2016/09/16 14:44:14 version 1.344.2.10.4.2, 2023/07/05 16:58:52
Line 51  use Apache::functionplotresponse(); Line 51  use Apache::functionplotresponse();
 use Apache::drawimage();  use Apache::drawimage();
 use Apache::Constants qw(:common);  use Apache::Constants qw(:common);
 use Apache::loncommon();  use Apache::loncommon();
   use Apache::lonparmset();
   use Apache::lonnavmaps();
 use Apache::lonlocal;  use Apache::lonlocal;
   use LONCAPA qw(:DEFAULT :match);
   use LONCAPA::ltiutils();
 use Time::HiRes qw( gettimeofday tv_interval );  use Time::HiRes qw( gettimeofday tv_interval );
 use HTML::Entities();  use HTML::Entities();
 use File::Copy();  use File::Copy();
   
 # FIXME - improve commenting  # FIXME - improve commenting
   
   my $registered_cleanup;
   
 BEGIN {  BEGIN {
     &Apache::lonxml::register_insert();      &Apache::lonxml::register_insert();
Line 188  sub proctor_checked_in { Line 193  sub proctor_checked_in {
     if ($type eq 'Task') {      if ($type eq 'Task') {
  my $version=$Apache::lonhomework::history{'resource.0.version'};   my $version=$Apache::lonhomework::history{'resource.0.version'};
  $key ="resource.$version.0.checkedin";   $key ="resource.$version.0.checkedin";
     } elsif ($type eq 'problem') {      } elsif (($type eq 'problem') || ($type eq 'tool')) {
  $key ='resource.0.checkedin';   $key ='resource.0.checkedin';
     }      }
     # backward compatability, used to be username@domain,       # backward compatability, used to be username@domain, 
Line 203  sub proctor_checked_in { Line 208  sub proctor_checked_in {
     return 1;      return 1;
  }   }
     }      }
       
     return 0;      return 0;
 }  }
   
Line 212  sub check_slot_access { Line 216  sub check_slot_access {
   
     # does it pass normal muster      # does it pass normal muster
     my ($status,$datemsg)=&check_access($id,$symb);      my ($status,$datemsg)=&check_access($id,$symb);
       
     my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);      my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
     if ($useslots ne 'resource' && $useslots ne 'map'       if ($useslots ne 'resource' && $useslots ne 'map' 
  && $useslots ne 'map_map') {   && $useslots ne 'map_map') {
Line 235  sub check_slot_access { Line 239  sub check_slot_access {
     $Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass') {      $Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass') {
     return ('SHOW_ANSWER');      return ('SHOW_ANSWER');
  }   }
       } elsif (($type eq 'problem') &&
                ($Apache::lonhomework::browse eq 'F') &&
                ($ENV{'REMOTE_ADDR'} eq '127.0.0.1') &&
                ($env{'form.grade_courseid'} eq $env{'request.course.id'}) &&
                (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}))) {
           return ($status,$datemsg);
     }      }
   
     my $availablestudent = &Apache::lonnet::EXT("resource.0.availablestudent",$symb);      my $availablestudent = &Apache::lonnet::EXT("resource.0.availablestudent",$symb);
Line 286  sub check_slot_access { Line 296  sub check_slot_access {
      || $Apache::lonhomework::history{"resource.0.solved"} =~ /^correct_/ );       || $Apache::lonhomework::history{"resource.0.solved"} =~ /^correct_/ );
  $checkedin =   $checkedin =
     $Apache::lonhomework::history{"resource.$version.0.checkedin"};      $Apache::lonhomework::history{"resource.$version.0.checkedin"};
     } elsif ($type eq 'problem') {      } elsif (($type eq 'problem') || ($type eq 'tool')) {
         $checkin = 'resource.0.checkedin';          $checkin = 'resource.0.checkedin';
  $checkedin  = $Apache::lonhomework::history{$checkin};   $checkedin  = $Apache::lonhomework::history{$checkin};
     }      }
Line 295  sub check_slot_access { Line 305  sub check_slot_access {
         my %slot=&Apache::lonnet::get_slot($checkinslot);          my %slot=&Apache::lonnet::get_slot($checkinslot);
         $consumed_uniq = $slot{'uniqueperiod'};          $consumed_uniq = $slot{'uniqueperiod'};
     }      }
     if ($type eq 'problem') {      if (($type eq 'problem') || ($type eq 'tool')) {
         if ((ref($partlist) eq 'ARRAY') && (@{$partlist} > 0)) {          if ((ref($partlist) eq 'ARRAY') && (@{$partlist} > 0)) {
             my ($numcorrect,$numgraded) = (0,0);              my ($numcorrect,$numgraded) = (0,0);
             foreach my $part (@{$partlist}) {              foreach my $part (@{$partlist}) {
Line 311  sub check_slot_access { Line 321  sub check_slot_access {
                         $earlyout = 1;                          $earlyout = 1;
                     }                      }
                 }                  }
                 if (($currtries == $maxtries) || ($is_correct)) {                  if ($currtries == $maxtries) {
                     $earlyout = 1;                      $earlyout = 1;
                 } else {                  } else {
                     $numgraded ++;                      $numgraded ++;
Line 354  sub check_slot_access { Line 364  sub check_slot_access {
     # However, the problem is not closed, and potentially, another slot might be      # However, the problem is not closed, and potentially, another slot might be
     # used to gain access to it to work on it, until the due date is reached, and the      # used to gain access to it to work on it, until the due date is reached, and the
     # problem then becomes CLOSED.  Therefore return the slotstatus -       # problem then becomes CLOSED.  Therefore return the slotstatus - 
     # (which will be one of: NOT_IN_A_SLOT, RESERVABLE, RESERVABLE_LATER, or NOTRESERVABLE.      # (which will be one of: NOT_IN_A_SLOT, RESERVABLE, RESERVABLE_LATER, or NOTRESERVABLE).
     if (!defined($slot_name) && $type eq 'problem') {  
       if (!defined($slot_name) && (($type eq 'problem') || ($type eq 'tool'))) {
         if ($slotstatus eq 'NOT_IN_A_SLOT') {          if ($slotstatus eq 'NOT_IN_A_SLOT') {
             if (!$num_usable_slots) {              if (!$num_usable_slots) {
                 if ($env{'request.course.id'}) {                  if ($env{'request.course.id'}) {
Line 439  sub check_slot_access { Line 450  sub check_slot_access {
     }      }
   
     if ( $is_correct) {      if ( $is_correct) {
  if ($type eq 'problem') {   if (($type eq 'problem') || ($type eq 'tool')) {
     return ($status);      return ($status);
  }   }
  return ('SHOW_ANSWER');   return ('SHOW_ANSWER');
Line 1056  sub editxmlmode { Line 1067  sub editxmlmode {
   
  $problem='';   $problem='';
     }      }
   
     if (($env{'form.problemmode'} eq 'saveeditxml') ||      if (($env{'form.problemmode'} eq 'saveeditxml') ||
         ($env{'form.problemmode'} eq 'saveviewxml') ||           ($env{'form.problemmode'} eq 'saveviewxml') ||
         ($env{'form.problemmode'} eq 'undoxml')) {          ($env{'form.problemmode'} eq 'undoxml')) {
  my $error=&handle_save_or_undo($request,\$problem,   my $error=&handle_save_or_undo($request,\$problem,
        \$env{'form.editxmltext'});         \$env{'form.editxmltext'});
Line 1107  sub editxmlmode { Line 1117  sub editxmlmode {
             <div class="LC_edit_problem_header">              <div class="LC_edit_problem_header">
               <div class="LC_edit_problem_header_title">'.                <div class="LC_edit_problem_header_title">'.
                 &mt('Problem Editing').' '.&Apache::loncommon::help_open_topic('Problem_Editor_XML_Index').                  &mt('Problem Editing').' '.&Apache::loncommon::help_open_topic('Problem_Editor_XML_Index').
                 </div><div class="LC_edit_actionbar" id="actionbar">';                  '</div><div class="LC_edit_actionbar" id="actionbar">';
   
     $result.='<input type="hidden" name="problemmode" value="saveedit" />'.      $result.='<input type="hidden" name="problemmode" value="saveedit" />'.
                   &Apache::structuretags::problem_edit_buttons('editxml');                    &Apache::structuretags::problem_edit_buttons('editxml');
Line 1130  sub editxmlmode { Line 1140  sub editxmlmode {
             &mt("Miscellaneous"), misc_datastructure());              &mt("Miscellaneous"), misc_datastructure());
     }      }
   
    $result .= Apache::lonmenu::create_submenu("#", "",      $result .= Apache::lonmenu::create_submenu("#", "",
         &mt("Help") . ' <img src="/adm/help/help.png" alt="' . &mt("Help") .          &mt("Help") . ' <img src="/adm/help/help.png" alt="' . &mt("Help") .
         '" style="vertical-align:text-bottom; height: auto; margin:0; "/>',          '" style="vertical-align:text-bottom; height: auto; margin:0; "/>',
         helpmenu_datastructure(),"");          helpmenu_datastructure(),"");
Line 1194  sub editxmlmode { Line 1204  sub editxmlmode {
 #    Render the page in whatever target desired.  #    Render the page in whatever target desired.
 #  #
 sub renderpage {  sub renderpage {
     my ($request,$file,$targets,$return_string) = @_;      my ($request,$file,$targets,$return_string,$donebuttonmsg) = @_;
   
     my @targets = @{$targets || [&get_target()]};      my @targets = @{$targets || [&get_target()]};
     &Apache::lonhomework::showhashsubset(\%env,'form.');      &Apache::lonhomework::showhashsubset(\%env,'form.');
Line 1245  sub renderpage { Line 1255  sub renderpage {
     if ($target eq 'analyze') {      if ($target eq 'analyze') {
  $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);   $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
  undef(%Apache::lonhomework::analyze);   undef(%Apache::lonhomework::analyze);
               } elsif ($target eq 'web') {
                   if ($donebuttonmsg) {
                       $result =~ s{</body>}{};
                       $result.= &Apache::loncommon::confirmwrapper(&Apache::lonhtmlcommon::confirm_success($donebuttonmsg,1))."\n</body>";
                   }
     }      }
     #my $td=&tv_interval($t0);      #my $td=&tv_interval($t0);
     #if ( $Apache::lonxml::debug) {      #if ( $Apache::lonxml::debug) {
Line 1272  sub finished_parsing { Line 1287  sub finished_parsing {
     undef($Apache::lonhomework::parsing_a_task);      undef($Apache::lonhomework::parsing_a_task);
 }  }
   
   
 # function extracted from get_template_html  # function extracted from get_template_html
 # returns "key" -> list  # returns "key" -> list
 # key: path of template  # key: path of template
Line 1369  sub newproblem { Line 1383  sub newproblem {
         &File::Copy::copy($templatefilename,$dest);          &File::Copy::copy($templatefilename,$dest);
         &renderpage($request,$dest);          &renderpage($request,$dest);
         return;          return;
       }
       my $errormsg;
     if ($env{'form.template'}) {      if ($env{'form.template'}) {
  my $file = $env{'form.template'};          my $file;
  my $dest = &Apache::lonnet::filelocation("",$request->uri);          my ($extension) = ($env{'form.template'} =~ /\.(\w+)$/);
  &File::Copy::copy($file,$dest);          if ($extension) {
  &renderpage($request,$dest);              my @files = &get_template_list($extension);
  return;              foreach my $poss (@files) {
                   if (ref($poss) eq 'ARRAY') {
                       if ($env{'form.template'} eq $poss->[0]) {
                           $file = $env{'form.template'};
                           last;
                       }
                   }
               }
               if ($file) {
                   my $dest = &Apache::lonnet::filelocation("",$request->uri);
                   &File::Copy::copy($file,$dest);
                   &renderpage($request,$dest);
                   return;
               } else {
                   $errormsg = '<p class="LC_error">'.&mt('Invalid template file.').'</p>';
               }
           } else {
               $errormsg = '<p class="LC_error">'.&mt('Invalid template file; template needs to be a .problem, .library, or .task file.').'</p>';
           }
     }      }
   
     my ($extension) = ($request->uri =~ m/\.(\w+)$/);      my ($extension) = ($request->uri =~ m/\.(\w+)$/);
Line 1392  sub newproblem { Line 1425  sub newproblem {
     } else {      } else {
  my $url=&HTML::Entities::encode($request->uri,'<>&"');   my $url=&HTML::Entities::encode($request->uri,'<>&"');
  my $dest = &Apache::lonnet::filelocation("",$request->uri);   my $dest = &Apache::lonnet::filelocation("",$request->uri);
  my $errormsg;  
  my $instructions;   my $instructions;
         my $brcrum = [{'href' => &Apache::loncommon::authorspace($request->uri),          my $brcrum = [{'href' => &Apache::loncommon::authorspace($request->uri),
                        'text' => 'Authoring Space'},                         'text' => 'Authoring Space'},
Line 1442  sub update_construct_style { Line 1474  sub update_construct_style {
     }      }
 }  }
   
   #
   # Sets interval for current user so time left will be zero, either for the entire folder
   # containing the current resource, or just the resource, depending on value of first item
   # in interval array retrieved from EXT("resource.0.interval");
   #
   sub zero_timer {
       my ($symb) = @_;
       my ($hastimeleft,$first_access,$now);
       my @interval=&Apache::lonnet::EXT("resource.0.interval",$symb);
       if (@interval > 1) {
           if ($interval[1] eq 'course') {
               return ('fail',&mt('Ending of timed events not supported for intervals set course-wide'));
           } else {
               my $now = time;
               my $first_access=&Apache::lonnet::get_first_access($interval[1],$symb);
               if ($first_access > 0) {
                   my ($timelimit,$donesuffix) = split(/_/,$interval[0],2);
                   if ($donesuffix =~ /^done(?:|\:[^\:]+\:)(.*)$/) {
                       my ($dummy,$proctor,$secret) = split(/_/,$1);
                       if (($proctor) && ($secret ne '')) {
                           my $key = $env{'form.LC_interval_done_proctorpass'};
                           $key =~ s/^\s+//;
                           $key =~ s/\s+$//;
                           if ($env{'form.LC_interval_done_proctorpass'} ne $secret) {
                               return ('fail',
                                      &mt('Incorrect key entered by proctor'));
                           }
                       }
                       if ($first_access+$timelimit > $now) {
                           my $done_time = $now - $first_access;
                           my $snum = 1;
                           if ($interval[1] eq 'map') {
                               $snum = 2;
                           }
                           my $result =
                               &Apache::lonparmset::storeparm_by_symb_inner($symb,'0_interval',
                                                                            $snum,$done_time,
                                                                            'date_interval',
                                                                            $env{'user.name'},
                                                                            $env{'user.domain'});
                           if ($result eq '') {
                               # Record action in "User Notes"
                               &Apache::lonmsg::store_instructor_comment(
                                   'Pressed Done button for symb:<br />'.$symb,
                                   $env{'user.name'}, $env{'user.domain'});
                               return ('ok');
                           } else {
                               return ('fail',&mt('Error ending timed event: [_1]',$result));
                           }
                       } else {
                           return ('fail',&mt('Timed event already ended'));
                       }
                   } else {
                       return ('fail',&mt('Timed event can not be ended before the time limit'));
                   }
               } else {
                   return ('fail',&mt('Timer not yet started for this timed event'));
               }
           }
       } else {
           return ('fail',&mt('No timer in use'));
       }
       return();
   }
   
 sub handler {  sub handler {
     #my $t0 = [&gettimeofday()];      #my $t0 = [&gettimeofday()];
Line 1454  sub handler { Line 1550  sub handler {
     my $file=&Apache::lonnet::filelocation("",$request->uri);      my $file=&Apache::lonnet::filelocation("",$request->uri);
   
     #check if we know where we are      #check if we know where we are
     if ($env{'request.course.fn'} && !&Apache::lonnet::symbread('','',1,1)) {       if ($env{'request.course.fn'} && !&Apache::lonnet::symbread('','',1,1)) {
  # if we are browsing we might not be able to know where we are   # if we are browsing we might not be able to know where we are
  if ($Apache::lonhomework::browse ne 'F' &&    if ($Apache::lonhomework::browse ne 'F' &&
     $env{'request.state'} ne "construct") {      $env{'request.state'} ne "construct") {
     #should know where we are, so ask      #should know where we are, so ask
     &unset_permissions();      &unset_permissions();
Line 1468  sub handler { Line 1564  sub handler {
  &unset_permissions();   &unset_permissions();
  return OK;   return OK;
     }      }
   
     &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:$Apache::lonhomework::modifygrades:$Apache::lonhomework::queuegrade");      &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:$Apache::lonhomework::modifygrades:$Apache::lonhomework::queuegrade");
     &Apache::lonxml::debug("Problem Mode ".$env{'form.problemmode'});      &Apache::lonxml::debug("Problem Mode ".$env{'form.problemmode'});
     my ($symb) = &Apache::lonnet::whichuser();      my ($symb) = &Apache::lonnet::whichuser();
Line 1498  sub handler { Line 1595  sub handler {
     &newproblem($request);      &newproblem($request);
  }   }
     } else {      } else {
           # Set the event timer to zero if the "done button" was clicked.  The button is
           # part of the doneButton form created in lonmenu.pm
           my ($donebuttonresult,$donemsg);
           if ($symb && $env{'form.LC_interval_done'} eq 'true') {
               ($donebuttonresult,$donemsg) = &zero_timer($symb);
               undef($env{'form.LC_interval_done'});
               undef($env{'form.LC_interval_done_proctorpass'});
           }
  # just render the page normally outside of construction space   # just render the page normally outside of construction space
  &Apache::lonxml::debug("not construct");   &Apache::lonxml::debug("not construct");
  &renderpage($request,$file);          undef(@Apache::lonhomework::ltipassback);
    &renderpage($request,$file,undef,undef,$donemsg);
           if (@Apache::lonhomework::ltipassback) {
               unless ($registered_cleanup) {
                   my $handlers = $request->get_handlers('PerlCleanupHandler');
                   $request->set_handlers('PerlCleanupHandler' =>
                                          [\&do_ltipassback,@{$handlers}]);
               }
           }
     }      }
     #my $td=&tv_interval($t0);      #my $td=&tv_interval($t0);
     #&Apache::lonxml::debug("Spent $td seconds processing");      #&Apache::lonxml::debug("Spent $td seconds processing");
Line 1645  sub default_xml_tag { Line 1758  sub default_xml_tag {
   
 sub helpmenu_datastructure {  sub helpmenu_datastructure {
   
     my $width = 500;      # filename, title, width, height
     my $height = 600;  
   
     my $helpers = [      my $helpers = [
                     ['Problem_LON-CAPA_Functions', &mt('Script Functions')],                      ['Problem_LON-CAPA_Functions.hlp', &mt('Script Functions'), 800, 600],
                     ['Greek_Symbols', &mt('Greek Symbols')],                      ['Greek_Symbols.hlp', &mt('Greek Symbols'), 500, 600],
                     ['Other_Symbols', &mt('Other Symbols')],                      ['Other_Symbols.hlp', &mt('Other Symbols'), 500, 600],
                     ['Authoring_Output_Tags', &mt('Output Tags')],                      ['Authoring_Output_Tags.hlp', &mt('Output Tags'), 800, 600],
                     ['Authoring_Multilingual_Problems', &mt('Languages')],                      ['Authoring_Multilingual_Problems.hlp', &mt('Languages'), 800, 600],
                    ];                     ];
   
     my $help_structure = [];      my $help_structure = [];
Line 1661  sub helpmenu_datastructure { Line 1772  sub helpmenu_datastructure {
     foreach my $count (0..(scalar(@{$helpers})-1)) {      foreach my $count (0..(scalar(@{$helpers})-1)) {
         my $filename = $helpers->[$count]->[0];          my $filename = $helpers->[$count]->[0];
         my $title = $helpers->[$count]->[1];          my $title = $helpers->[$count]->[1];
         my $href = &HTML::Entities::encode("javascript:openMyModal('/adm/help/$filename.hlp',$width,$height,'yes');");          my $width = $helpers->[$count]->[2];
           my $height = $helpers->[$count]->[3];
           if ($width eq '') {
               $width = 500;
           }
           if ($height eq '') {
               $height = 600;
           }
           my $href = &HTML::Entities::encode("javascript:openMyModal('/adm/help/$filename',$width,$height,'yes');");
         push @{$help_structure}, [$href, $title, undef];          push @{$help_structure}, [$href, $title, undef];
     }      }
   
Line 1679  sub convert_for_js { Line 1798  sub convert_for_js {
     return $return;      return $return;
 }  }
   
   sub do_ltipassback {
       if (@Apache::lonhomework::ltipassback) {
           foreach my $item (@Apache::lonhomework::ltipassback) {
               if (ref($item) eq 'HASH') {
                   if ((ref($item->{'lti'}) eq 'HASH') && ($item->{'cid'} =~ /^($match_domain)_($match_courseid)$/)) {
                       my ($cdom,$cnum) = ($1,$2);
                       my $msgformat = $item->{'lti'}->{'passbackformat'};
                       my $sigmethod = 'HMAC-SHA1';
                       my $ltinum = $item->{'ltinum'};
                       my $id = $item->{'pbid'};
                       my $url = $item->{'pburl'};
                       my $type = $item->{'pbtype'};
                       my $scope = $item->{'scope'};
                       my $map = $item->{'ltimap'};
                       my $symb = $item->{'ltisymb'};
                       my $uname = $item->{'uname'};
                       my $udom = $item->{'udom'};
                       my $keynum = $item->{'lti'}->{'cipher'};
                       my $crsdef = $item->{'crsdef'};
                       my $scoretype = $item->{'format'};
                       my ($total,$possible);
                       if ($scope eq 'resource') {
                           $total = $item->{'total'};
                           $possible = $item->{'possible'};
                       } elsif ($scope eq 'map') {
                           ($total,$possible) = &get_lti_score($uname,$udom,$map);
                       } elsif ($scope eq 'course') {
                           ($total,$possible) = &get_lti_score($uname,$udom);
                       }
                       if (($id ne '') && ($url ne '') && ($possible)) {
                           &LONCAPA::ltiutils::send_grade($cdom,$cnum,$crsdef,$type,$ltinum,$keynum,$id,$url,$scoretype,$sigmethod,$msgformat,$total,$possible);
                       }
                   }
               }
           }
           undef(@Apache::lonhomework::ltipassback);
       }
   }
   
   sub get_lti_score {
       my ($uname,$udom,$mapurl) = @_;
       my $navmap = Apache::lonnavmaps::navmap->new($uname,$udom);
       if (ref($navmap)) {
           my $iterator;
           if ($mapurl ne '') {
               my $map = $navmap->getResourceByUrl($mapurl);
               my $firstres = $map->map_start();
               my $finishres = $map->map_finish();
               $iterator = $navmap->getIterator($firstres,$finishres,undef,1);
           } else {
               $iterator = $navmap->getIterator(undef,undef,undef,1);
           }
           if (ref($iterator)) {
               my $depth = 1;
               my $total = 0;
               my $possible = 0;
               $iterator->next(); # ignore first BEGIN_MAP
               my $curRes = $iterator->next();
               while ( $depth > 0 ) {
                   if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
                   if ($curRes == $iterator->END_MAP()) { $depth--; }
                   if (ref($curRes) && $curRes->is_gradable() && !$curRes->randomout) {
                       my $parts = $curRes->parts();
                       foreach my $part (@{$parts}) {
                           next if ($curRes->solved($part) eq 'excused');
                           $total += $curRes->weight($part) * $curRes->awarded($part);
                           $possible += $curRes->weight($part);
                       }
                   }
                   $curRes = $iterator->next();
               }
               if ($total > $possible) {
                   $total = $possible;
               }
               return ($total,$possible);
           }
       }
       return;
   }
   
 1;  1;
 __END__  __END__

Removed from v.1.344.2.6  
changed lines
  Added in v.1.344.2.10.4.2


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