File:
[LON-CAPA] /
loncom /
interface /
lonrelrequtils.pm
Revision
1.2:
download - view:
text,
annotated -
select for diffs
Fri Jun 13 01:48:26 2014 UTC (10 years, 2 months ago) by
raeburn
Branches:
MAIN
CVS tags:
version_2_11_X,
version_2_11_5_msu,
version_2_11_5,
version_2_11_4_uiuc,
version_2_11_4_msu,
version_2_11_4,
version_2_11_3_uiuc,
version_2_11_3_msu,
version_2_11_3,
version_2_11_2_uiuc,
version_2_11_2_msu,
version_2_11_2_educog,
version_2_11_2,
version_2_11_1,
version_2_11_0,
HEAD
- Setting the printstartdate parameter to a date/time in the future, or
setting the printstartdate parameter to a date/time in the past
(used to prevent printout generation by students) require the course's
homeserver to have LON-CAPA 2.11 installed.
1: #!/usr/bin/perl
2: # The LearningOnline Network
3: #
4: # $Id: lonrelrequtils.pm,v 1.2 2014/06/13 01:48:26 raeburn Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: #################################################
29:
30: =pod
31:
32: =head1 NAME
33:
34: lonrelrequtils.pm
35:
36: =head1 SYNOPSIS
37:
38: Contains utilities used to determine the LON-CAPA version
39: requirement in a course, based on course type, parameters,
40: responsetypes, and communication blocking events.
41:
42: =head1 DESCRIPTION
43:
44: lonrelrequtilities.pm includes a main subroutine:
45: get_release_req() which will return the current major
46: version and minor version requirement (if it exists).
47:
48: =head1 SUBROUTINES
49:
50: =over
51:
52: =item &init_global_hashes()
53:
54: Initializes package hashes containing version requirements for
55: parameters, responsetypes, course types, anonsurvey
56: parameter, and randomizetry parameter.
57:
58: =item &get_release_req()
59:
60: Returns current major version and minor version requirements for a course,
61: based on: coursetype, parameters in use, responsetypes in use in course
62: content, and communication blocking features in use in blocks with end dates
63: in the future, or in blocks triggered by activation of a timer in a timed quiz.
64:
65: Inputs: 5
66:
67: =over
68:
69: =item $cnum - course "number"
70:
71: =item $cdom - course domain
72:
73: =item $crstype - course type: Community or Course
74:
75: =item $readmap - boolean; if true, read course's top level map, and any
76: included maps recursively.
77:
78: =item $globals_set - boolean: if false, call init_global_hashes
79:
80: =back
81:
82:
83: =item ¶meter_constraints()
84:
85: Returns major version and minor version requirements for a course,
86: based on parameters in use in the course. (Parameters which have
87: version requirements are listed in /home/httpd/lonTabs/releaseslist.xml
88:
89: Inputs: 2
90:
91: =over
92:
93: =item $cnum - course "number"
94:
95: =item $cdom - course domain
96:
97: =back
98:
99:
100: =item &coursetype_constraints()
101:
102: Returns major version and minor version requirements for a course,
103: taking into account course type (Community or Course).
104:
105: Inputs: 5
106:
107: =over
108:
109: =item $cnum - course "number"
110:
111: =item $cdom - course domain
112:
113: =item $crstype - course type: Community or Course
114:
115: =item $reqdmajor - major version requirements based on constraints
116: considered so far (parameters).
117:
118: =item $reqdminor - minor version requirements based on constraints
119: considered so far (parameters).
120:
121: =back
122:
123:
124: =item &commblock_constraints()
125:
126: Returns major version and minor version requirements for a course,
127: taking into account use of communication blocking (blocks for
128: printouts, specified folders/resources, and/or triggering of block
129: by a student starting a timed quiz.
130:
131: Inputs: 4
132:
133: =over
134:
135: =item $cnum - course "number"
136:
137: =item $cdom - course domain
138:
139: =item $reqdmajor - major version requirements based on constraints
140: considered so far (parameters and course type).
141:
142: =item $reqdminor - minor version requirements based on constraints
143: considered so far (parameters and course type).
144:
145: =back
146:
147:
148: =item &coursecontent_constraints()
149:
150: Returns major version and minor version requirements for a course,
151: taking into responsetypes in use in published assessment items
152: imported into a course.
153:
154: Inputs: 4
155:
156: =over
157:
158: =item $cnum - course "number"
159:
160: =item $cdom - course domain
161:
162: =item $reqdmajor - major version requirements based on constraints
163: considered so far (parameters, course type, blocks).
164:
165: =item $reqdminor - minor version requirements based on constraints
166: considered so far (parameters, course type, blocks).
167:
168: =back
169:
170:
171: =item &update_reqd_loncaparev()
172:
173: Returns major version and minor version requirements for a course,
174: taking into account new constraint type.
175:
176: Inputs: 4
177:
178: =over
179:
180: =item $major - major version requirements from new constraint type
181:
182: =item $minor - minor version requirements from new constraint type
183:
184: =item $reqdmajor - major version requirements from constraints
185: considered so far.
186:
187: =item $reqdminor - minor version requirements from constraints
188: considered so far.
189:
190: =back
191:
192:
193: =item &read_paramdata()
194:
195: Returns a reference to a hash populated with parameter settings in a
196: course (set both generally, and for specific students).
197:
198: Inputs: 2
199:
200: =over
201:
202: =item $cnum - course "number"
203:
204: =item $cdom - course domain
205:
206: =back
207:
208:
209: =item &modify_course_relreq()
210:
211: Updates course's minimum version requirement (internal.releaserequired) in
212: course's environment.db, and in user's current session, and in course's
213: record in nohist_courseids.db on course's home server. This can include
214: deleting an existing version requirement, downgrading to an earlier version,
215: or updating to a newer version.
216:
217: Note: if the current server's LON-CAPA version is older than the course's
218: current version requirement, and a downgrade to an earlier version is being
219: proposed, the change will NOT be made, because of the possibility that the
220: current server has not checked for an attribute only available with a more
221: recent version of LON-CAPA.
222:
223: Inputs: 9
224:
225: =over
226:
227: =item $newmajor - (optional) major version requirements
228:
229: =item $newminor - (optional) minor version requirements
230:
231: =item $cnum - course "number"
232:
233: =item $cdom - course domain
234:
235: =item $chome - lonHostID of course's home server
236:
237: =item $crstype - course type: Community or Course
238:
239: =item $cid - course ID
240:
241: =item $readmap - boolean; if true, read course's top level map, and any
242: included maps recursively.
243:
244: =item $getrelreq - boolean; if true, call &get_release_req() to
245: return the current major version and minor version requirements.
246: (needed if optional args: $newmajor and $newminor are not passed).
247:
248: =back
249:
250: =back
251:
252: =cut
253:
254: #################################################
255:
256: package Apache::lonrelrequtils;
257:
258: use strict;
259: use Apache::lonnet;
260: use Apache::loncommon();
261: use Apache::lonuserstate();
262: use Apache::loncoursedata();
263: use Apache::lonnavmaps();
264: use LONCAPA qw(:DEFAULT :match);
265:
266: sub init_global_hashes {
267: %Apache::lonrelrequtils::checkparms = ();
268: %Apache::lonrelrequtils::checkresponsetypes = ();
269: %Apache::lonrelrequtils::checkcrstypes = ();
270: %Apache::lonrelrequtils::anonsurvey = ();
271: %Apache::lonrelrequtils::randomizetry = ();
272:
273: foreach my $key (keys(%Apache::lonnet::needsrelease)) {
274: my ($item,$name,$value) = split(/:/,$key);
275: if ($item eq 'parameter') {
276: if (ref($Apache::lonrelrequtils::checkparms{$name}) eq 'ARRAY') {
277: unless(grep(/^\Q$name\E$/,@{$Apache::lonrelrequtils::checkparms{$name}})) {
278: push(@{$Apache::lonrelrequtils::checkparms{$name}},$value);
279: }
280: } else {
281: push(@{$Apache::lonrelrequtils::checkparms{$name}},$value);
282: }
283: } elsif ($item eq 'resourcetag') {
284: if ($name eq 'responsetype') {
285: $Apache::lonrelrequtils::checkresponsetypes{$value} = $Apache::lonnet::needsrelease{$key}
286: }
287: } elsif ($item eq 'course') {
288: if ($name eq 'crstype') {
289: $Apache::lonrelrequtils::checkcrstypes{$value} = $Apache::lonnet::needsrelease{$key};
290: }
291: }
292: }
293: ($Apache::lonrelrequtils::anonsurvey{major},$Apache::lonrelrequtils::anonsurvey{minor}) =
294: split(/\./,$Apache::lonnet::needsrelease{'parameter:type:anonsurvey'});
295: ($Apache::lonrelrequtils::randomizetry{major},$Apache::lonrelrequtils::randomizetry{minor}) =
296: split(/\./,$Apache::lonnet::needsrelease{'parameter:type:randomizetry'});
297: return;
298: }
299:
300: sub get_release_req {
301: my ($cnum,$cdom,$crstype,$readmap,$globals_set) = @_;
302: if ($readmap) {
303: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
304: }
305: unless ($globals_set) {
306: &init_global_hashes();
307: }
308: # check all parameters
309: my ($reqdmajor,$reqdminor) = ¶meter_constraints($cnum,$cdom);
310:
311: # check course type
312: ($reqdmajor,$reqdminor) = &coursetype_constraints($cnum,$cdom,$crstype,$reqdmajor,
313: $reqdminor);
314: # check communication blocks
315: ($reqdmajor,$reqdminor) = &commblock_constraints($cnum,$cdom,$reqdmajor,$reqdminor);
316:
317: # check course contents
318: ($reqdmajor,$reqdminor) = &coursecontent_constraints($cnum,$cdom,$reqdmajor,$reqdminor);
319: return ($reqdmajor,$reqdminor);
320: }
321:
322: sub parameter_constraints {
323: my ($cnum,$cdom) = @_;
324: my ($reqdmajor,$reqdminor);
325: my $resourcedata=&read_paramdata($cnum,$cdom);
326: my $now = time;
327: if (ref($resourcedata) eq 'HASH') {
328: foreach my $key (keys(%{$resourcedata})) {
329: foreach my $item (keys(%Apache::lonrelrequtils::checkparms)) {
330: if ($key =~ /(\Q$item\E)$/) {
331: if (ref($Apache::lonrelrequtils::checkparms{$item}) eq 'ARRAY') {
332: my $value = $resourcedata->{$key};
333: if ($item eq 'examcode') {
334: if (&Apache::lonnet::validCODE($value)) {
335: $value = 'valid';
336: } else {
337: $value = '';
338: }
339: } elsif ($item eq 'printstartdate') {
340: if ($value =~ /^\d+$/) {
341: if ($value > $now) {
342: $value = 'future';
343: }
344: }
345: } elsif ($item eq 'printenddate') {
346: if ($value =~ /^\d+$/) {
347: if ($value < $now) {
348: $value = 'past';
349: }
350: }
351: }
352: if (grep(/^\Q$value\E$/,@{$Apache::lonrelrequtils::checkparms{$item}})) {
353: my ($major,$minor) = split(/\./,$Apache::lonnet::needsrelease{'parameter:'.$item.':'.$value});
354: ($reqdmajor,$reqdminor) =
355: &update_reqd_loncaparev($major,$minor,$reqdmajor,$reqdminor);
356: }
357: }
358: }
359: }
360: }
361: }
362: return ($reqdmajor,$reqdminor);
363: }
364:
365: sub coursetype_constraints {
366: my ($cnum,$cdom,$crstype,$reqdmajor,$reqdminor) = @_;
367: if (defined($Apache::lonrelrequtils::checkcrstypes{$crstype})) {
368: my ($major,$minor) = split(/\./,$Apache::lonrelrequtils::checkcrstypes{$crstype});
369: ($reqdmajor,$reqdminor) =
370: &update_reqd_loncaparev($major,$minor,$reqdmajor,$reqdminor);
371: }
372: return ($reqdmajor,$reqdminor);
373: }
374:
375: sub commblock_constraints {
376: my ($cnum,$cdom,$reqdmajor,$reqdminor) = @_;
377: my %comm_blocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
378: my $now = time;
379: if (keys(%comm_blocks) > 0) {
380: foreach my $block (keys(%comm_blocks)) {
381: if ($block =~ /^firstaccess____(.+)$/) {
382: my ($major,$minor) = split(/\./,$Apache::lonnet::needsrelease{'course:commblock:timer'});
383: ($reqdmajor,$reqdminor) = &update_reqd_loncaparev($major,$minor,$reqdmajor,$reqdminor);
384: last;
385: } elsif ($block =~ /^(\d+)____(\d+)$/) {
386: my ($start,$end) = ($1,$2);
387: next if ($end < $now);
388: }
389: if (ref($comm_blocks{$block}) eq 'HASH') {
390: if (ref($comm_blocks{$block}{'blocks'}) eq 'HASH') {
391: if (ref($comm_blocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
392: if (keys(%{$comm_blocks{$block}{'blocks'}{'docs'}}) > 0) {
393: my ($major,$minor) = split(/\./,$Apache::lonnet::needsrelease{'course:commblock:docs'});
394: ($reqdmajor,$reqdminor) = &update_reqd_loncaparev($major,$minor,$reqdmajor,$reqdminor);
395: last;
396: }
397: }
398: if ($comm_blocks{$block}{'blocks'}{'printout'} eq 'on') {
399: my ($major,$minor) = split(/\./,$Apache::lonnet::needsrelease{'course:commblock:printout'});
400: ($reqdmajor,$reqdminor) = &update_reqd_loncaparev($major,$minor,$reqdmajor,$reqdminor);
401: last;
402: }
403: }
404: }
405: }
406: }
407: return ($reqdmajor,$reqdminor);
408: }
409:
410: sub coursecontent_constraints {
411: my ($cnum,$cdom,$reqdmajor,$reqdminor) = @_;
412: my $navmap = Apache::lonnavmaps::navmap->new();
413: if (defined($navmap)) {
414: my %anonsubmissions = &Apache::lonnet::dump('nohist_anonsurveys',
415: $cdom,$cnum);
416: my %randomizetrysubm = &Apache::lonnet::dump('nohist_randomizetry',
417: $cdom,$cnum);
418: my %allresponses;
419: my ($anonsurv_subm,$randbytry_subm);
420: foreach my $res ($navmap->retrieveResources(undef,sub { $_[0]->is_problem() },1,0)) {
421: my %responses = $res->responseTypes();
422: foreach my $key (keys(%responses)) {
423: next unless(exists($Apache::lonrelrequtils::checkresponsetypes{$key}));
424: $allresponses{$key} += $responses{$key};
425: }
426: my @parts = @{$res->parts()};
427: my $symb = $res->symb();
428: foreach my $part (@parts) {
429: if (exists($anonsubmissions{$symb."\0".$part})) {
430: $anonsurv_subm = 1;
431: }
432: if (exists($randomizetrysubm{$symb."\0".$part})) {
433: $randbytry_subm = 1;
434: }
435: }
436: }
437: foreach my $key (keys(%allresponses)) {
438: my ($major,$minor) = split(/\./,$Apache::lonrelrequtils::checkresponsetypes{$key});
439: ($reqdmajor,$reqdminor) = &update_reqd_loncaparev($major,$minor,$reqdmajor,$reqdminor);
440: }
441: if ($anonsurv_subm) {
442: ($reqdmajor,$reqdminor) = &update_reqd_loncaparev($Apache::lonrelrequtils::anonsurvey{major},
443: $Apache::lonrelrequtils::anonsurvey{minor},$reqdmajor,$reqdminor);
444: }
445: if ($randbytry_subm) {
446: ($reqdmajor,$reqdminor) = &update_reqd_loncaparev($Apache::lonrelrequtils::randomizetry{major},
447: $Apache::lonrelrequtils::randomizetry{minor},$reqdmajor,$reqdminor);
448: }
449: }
450: return ($reqdmajor,$reqdminor);
451: }
452:
453: sub update_reqd_loncaparev {
454: my ($major,$minor,$reqdmajor,$reqdminor) = @_;
455: if (($major ne '' && $major !~ /\D/) & ($minor ne '' && $minor !~ /\D/)) {
456: if ($reqdmajor eq '' || $reqdminor eq '') {
457: $reqdmajor = $major;
458: $reqdminor = $minor;
459: } elsif (($major > $reqdmajor) ||
460: ($major == $reqdmajor && $minor > $reqdminor)) {
461: $reqdmajor = $major;
462: $reqdminor = $minor;
463: }
464: }
465: return ($reqdmajor,$reqdminor);
466: }
467:
468: sub read_paramdata {
469: my ($cnum,$cdom)=@_;
470: my $resourcedata=&Apache::lonnet::get_courseresdata($cnum,$cdom);
471: my $classlist=&Apache::loncoursedata::get_classlist();
472: foreach my $student (keys(%{$classlist})) {
473: if ($student =~/^($LONCAPA::match_username)\:($LONCAPA::match_domain)$/) {
474: my ($tuname,$tudom)=($1,$2);
475: my $useropt=&Apache::lonnet::get_userresdata($tuname,$tudom);
476: foreach my $userkey (keys(%{$useropt})) {
477: if ($userkey=~/^\Q$cdom\E_\Q$cnum\E/) {
478: my $newkey=$userkey;
479: $newkey=~s/^(\Q$cdom\E_\Q$cnum\E\.)/$1\[useropt\:$tuname\:$tudom\]\./;
480: $$resourcedata{$newkey}=$$useropt{$userkey};
481: }
482: }
483: }
484: }
485: return $resourcedata;
486: }
487:
488: sub modify_course_relreq {
489: my ($newmajor,$newminor,$cnum,$cdom,$chome,$crstype,$cid,$readmap,$getrelreq) = @_;
490: if ($cnum eq '' || $cdom eq '' || $chome eq '' || $crstype eq '' || $cid eq '') {
491: $cid = $env{'request.course.id'};
492: $cdom = $env{'course.'.$cid.'.domain'};
493: $cnum = $env{'course.'.$cid.'.num'};
494: $chome = $env{'course.'.$cid.'.home'};
495: $crstype = $env{'course.'.$cid.'.type'};
496: if ($crstype eq '') {
497: $crstype = 'Course';
498: }
499: }
500: if ($getrelreq) {
501: ($newmajor,$newminor) = &get_release_req($cnum,$cdom,$crstype,$readmap);
502: }
503: my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired');
504: my $needsupdate;
505: if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
506: if (($newmajor ne '') && ($newminor ne '')) {
507: $needsupdate = 1;
508: }
509: } else {
510: my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
511: my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
512: my $serverdom = $Apache::lonnet::perlvar{'lonDefDomain'};
513: my $serverrev = &Apache::lonnet::get_server_loncaparev($serverdom,$lonhost);
514: my ($servermajor,$serverminor) = split(/\./,$serverrev);
515: unless (($currmajor > $servermajor) || (($currmajor == $servermajor) && ($currminor > $serverminor))) {
516: if (($currmajor != $newmajor) || ($currminor != $newminor)) {
517: $needsupdate = 1;
518: }
519: }
520: }
521: if ($needsupdate) {
522: my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
523: my $result;
524: if (($newmajor eq '') && ($newminor eq '')) {
525: $result = &Apache::lonnet::del('environment',['internal.releaserequired'],$cdom,$cnum);
526: if ($result eq 'ok') {
527: &Apache::lonnet::delenv('course.'.$cid.'.internal.releaserequired');
528: $crsinfo{$cid}{'releaserequired'} = '';
529: }
530: } else {
531: my %needshash = (
532: 'internal.releaserequired' => $newmajor.'.'.$newminor,
533: );
534: $result = &Apache::lonnet::put('environment',\%needshash,$cdom,$cnum);
535: if ($result eq 'ok') {
536: &Apache::lonnet::appenv({'course.'.$cid.'.internal.releaserequired' => $newmajor.'.'.$newminor});
537: if (ref($crsinfo{$cid}) eq 'HASH') {
538: $crsinfo{$cid}{'releaserequired'} = $newmajor.'.'.$newminor
539: }
540: }
541: }
542: if ($result eq 'ok') {
543: &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
544: }
545: }
546: return;
547: }
548:
549: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>