Parent Directory
|
Revision Log
Revision 6149 - (view) (download) (as text)
| 1 : | sh002i | 798 | ################################################################################ |
| 2 : | sh002i | 1663 | # WeBWorK Online Homework Delivery System |
| 3 : | sh002i | 5319 | # Copyright © 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/ |
| 4 : | apizer | 6149 | # $CVSHeader: webwork2/lib/WeBWorK/DB/Utils.pm,v 1.24 2007/08/13 22:59:56 sh002i Exp $ |
| 5 : | sh002i | 1663 | # |
| 6 : | # This program is free software; you can redistribute it and/or modify it under | ||
| 7 : | # the terms of either: (a) the GNU General Public License as published by the | ||
| 8 : | # Free Software Foundation; either version 2, or (at your option) any later | ||
| 9 : | # version, or (b) the "Artistic License" which comes with this package. | ||
| 10 : | # | ||
| 11 : | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 : | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 13 : | # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the | ||
| 14 : | # Artistic License for more details. | ||
| 15 : | sh002i | 798 | ################################################################################ |
| 16 : | |||
| 17 : | package WeBWorK::DB::Utils; | ||
| 18 : | sh002i | 818 | use base qw(Exporter); |
| 19 : | sh002i | 798 | |
| 20 : | =head1 NAME | ||
| 21 : | |||
| 22 : | WeBWorK::DB::Utils - useful utilities for the database modules. | ||
| 23 : | |||
| 24 : | =cut | ||
| 25 : | |||
| 26 : | use strict; | ||
| 27 : | use warnings; | ||
| 28 : | |||
| 29 : | our @EXPORT = (); | ||
| 30 : | our @EXPORT_OK = qw( | ||
| 31 : | sh002i | 944 | global2user |
| 32 : | user2global | ||
| 33 : | malsyned | 1027 | initializeUserProblem |
| 34 : | sh002i | 4568 | make_vsetID |
| 35 : | sh002i | 4787 | make_vsetID_sql |
| 36 : | sh002i | 4568 | grok_vsetID |
| 37 : | grok_setID_from_vsetID_sql | ||
| 38 : | grok_versionID_from_vsetID_sql | ||
| 39 : | sh002i | 798 | ); |
| 40 : | sh002i | 4787 | |
| 41 : | sh002i | 944 | sub global2user($$) { |
| 42 : | my ($userRecordClass, $GlobalRecord) = @_; | ||
| 43 : | my $UserRecord = $userRecordClass->new(); | ||
| 44 : | foreach my $field ($GlobalRecord->FIELDS()) { | ||
| 45 : | $UserRecord->$field($GlobalRecord->$field()); | ||
| 46 : | } | ||
| 47 : | return $UserRecord; | ||
| 48 : | } | ||
| 49 : | |||
| 50 : | sub user2global($$) { | ||
| 51 : | my ($globalRecordClass, $UserRecord) = @_; | ||
| 52 : | my $GlobalRecord = $globalRecordClass->new(); | ||
| 53 : | foreach my $field ($GlobalRecord->FIELDS()) { | ||
| 54 : | $GlobalRecord->$field($UserRecord->$field()); | ||
| 55 : | } | ||
| 56 : | return $GlobalRecord; | ||
| 57 : | } | ||
| 58 : | |||
| 59 : | malsyned | 1026 | # Populate a user record with sane defaults and a random seed |
| 60 : | # This function edits the record in place, so you can discard | ||
| 61 : | # the return value. | ||
| 62 : | sub initializeUserProblem { | ||
| 63 : | sh002i | 4739 | my ($userProblem, $seed) = @_; |
| 64 : | $seed = int rand 5000 unless defined $seed; | ||
| 65 : | malsyned | 1026 | $userProblem->status(0.0); |
| 66 : | $userProblem->attempted(0); | ||
| 67 : | $userProblem->num_correct(0); | ||
| 68 : | $userProblem->num_incorrect(0); | ||
| 69 : | sh002i | 4739 | $userProblem->problem_seed($seed); |
| 70 : | apizer | 6149 | $userProblem->sub_status(0.0); |
| 71 : | malsyned | 1026 | |
| 72 : | return $userProblem; | ||
| 73 : | } | ||
| 74 : | |||
| 75 : | sh002i | 4568 | ################################################################################ |
| 76 : | # versioning utilities | ||
| 77 : | ################################################################################ | ||
| 78 : | |||
| 79 : | sub make_vsetID($$) { | ||
| 80 : | my ($setID, $versionID) = @_; | ||
| 81 : | sh002i | 4586 | return "$setID,v$versionID"; |
| 82 : | sh002i | 4568 | } |
| 83 : | |||
| 84 : | sh002i | 4813 | # does not quote $setID and $versionID, because they could be strings, qualified |
| 85 : | # or unqualified field names, or complex expression | ||
| 86 : | sh002i | 4787 | sub make_vsetID_sql { |
| 87 : | my ($setID, $versionID) = @_; | ||
| 88 : | return "CONCAT($setID,',v',$versionID)"; | ||
| 89 : | } | ||
| 90 : | |||
| 91 : | sh002i | 4568 | sub grok_vsetID($) { |
| 92 : | my ($vsetID) = @_; | ||
| 93 : | sh002i | 4586 | my ($setID, $versionID) = $vsetID =~ /([^,]+)(?:,v(.*))?/; |
| 94 : | sh002i | 4568 | return $setID, $versionID; |
| 95 : | } | ||
| 96 : | |||
| 97 : | sh002i | 4813 | # does not quote $field, because it could be a string, a qualified or |
| 98 : | # unqualified field name, or a complex expression | ||
| 99 : | sh002i | 4568 | sub grok_setID_from_vsetID_sql($) { |
| 100 : | my ($field) = @_; | ||
| 101 : | sh002i | 4813 | return "SUBSTRING($field,1,INSTR($field,',v')-1)"; |
| 102 : | sh002i | 4568 | } |
| 103 : | |||
| 104 : | sh002i | 4813 | # does not quote $field, because it could be a string, a qualified or |
| 105 : | # unqualified field name, or a complex expression | ||
| 106 : | sh002i | 4568 | sub grok_versionID_from_vsetID_sql($) { |
| 107 : | my ($field) = @_; | ||
| 108 : | sh002i | 4586 | # the "+0" casts the resulting value as a number |
| 109 : | sh002i | 4813 | return "(SUBSTRING($field,INSTR($field,',v')+2)+0)"; |
| 110 : | sh002i | 4568 | } |
| 111 : | |||
| 112 : | sh002i | 798 | 1; |
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |