[system] / trunk / wwmoodle / wwassignment / lib.php Repository:
ViewVC logotype

View of /trunk/wwmoodle/wwassignment/lib.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4187 - (download) (as text) (annotate)
Thu Jun 29 16:10:43 2006 UTC (13 years, 7 months ago) by gage
File size: 19724 byte(s)
changed grade_method to gradingmethod in the wwassignment_grades() subroutine.

    1 <?php
    2 // $Id: lib.php,v 1.3 2006-06-29 16:10:43 gage Exp $
    3 //require_once("DB.php");
    4 function debug_log($obj) {
    5   $fh = fopen("/home/gage/moodle_debug", "w");
    6   //fwrite($fh,  "wwmoodle\n");
    7   $struct = print_r($obj, true);
    8   fwrite($fh, $struct);
    9   fwrite($fh, "\n");
   10   fclose($fh);
   11   //MEG
   12 }
   13 //MEG
   14 $path = ini_get('include_path');
   15 //debug_log($path);
   16 //ini_set('include_path', $path . ':/usr/local/lib/php/pear/DB');
   17 //ini_set('include_path', $path . 'lib/adodb');
   18 //require_once("PEAR.php");
   19 //require_once("DB.php");
   20 require_once($CFG->libdir.'/adodb/adodb-pear.inc.php');
   21 //endMEG
   22 
   23 
   24 
   25 //MEG  from old wwmoodle/lib.php
   26 /**
   27  * These defines exist simply for easy of referencing them in functions.
   28  */
   29 /**
   30  * The location of the webwork courses directory.
   31  * The user that this script executes as must have write access to this directory.
   32  * This must end in a /!
   33  */
   34 define('WWASSIGNMENT_WEBWORK_COURSES', $CFG->wwassignment_webworkcourses);
   35 /**
   36  * The URL of the WeBWorK install.
   37  */
   38 define('WWASSIGNMENT_WEBWORK_URL', $CFG->wwassignment_webworkurl);
   39 
   40 
   41 
   42 
   43 
   44 
   45 /// Library of functions and constants for module wwassignment
   46 
   47 /**
   48  * The URL of your WeBWorK installation.
   49  */
   50 define('WW_TABLE_PREFIX', 'webwork');
   51 define('WWASSIGNMENT_WEBWORK_URL', $CFG->wwassignment_webwork_url);
   52 /**
   53  * The PEAR::DB connection string to connect to the WeBWorK database.
   54  * This is in the form:
   55  * type://username:password@host/dbname
   56  * Where type is usually either 'mysql', 'mysqli', or 'pgsql'
   57  */
   58 define('WWASSIGNMENT_WEBWORK_DB', $CFG->wwmoodle_webwork_db);
   59 
   60 
   61 function wwassignment_gradeMethods() {
   62 
   63   return array(
   64     0 => array('name' => get_string("gradeSimple", "wwassignment"), 'formula' => '$fGrade += ($p[\'num_correct\'] > 0) ? 1 : 0;'),
   65     1 => array('name' => get_string("gradeDeductRetry", "wwassignment"), 'formula' => '$fGrade += $p[\'num_correct\']/$p[\'attempted\'];'),
   66   );
   67 
   68 }
   69 
   70 /**
   71  * Prints out a select box allowing a teacher to select how WeBWorK grades will be calculated.
   72  * @param int $iGradeMethod The current grade method.
   73  * @return void
   74  */
   75 function wwassignment_printGradeMethodSelect($iGradeMethod='-1') {
   76     // debug_log("printGrademethodSelect called");
   77   $wwassignment_gradeMethods = wwassignment_gradeMethods();
   78   print("<select id='gradingmethod' name='gradingmethod'>\n");
   79   foreach( $wwassignment_gradeMethods as $k=>$g ) {
   80     print("\t<option value='$k'");
   81     if( $k == $iGradeMethod ) {
   82       print(" selected='selected'");
   83     }
   84     print(">$g[name]</option>\n");
   85   }
   86 }
   87 
   88 
   89 
   90 
   91 /**
   92  * Maps a course ID number to it's (sanatized) shortname.
   93  * @param int $iCourseID The ID of the course.
   94  * @return string The shortname of the course, with unsafe characters removed. If the courseID is not found, null is returned.
   95  */
   96 function wwassignment_courseIdToShortName($iCourseId) {
   97   $c = get_record('course', 'id', $iCourseId);
   98   if( ! $c ) {
   99     return null;
  100   }
  101   $shortname = preg_replace("/[^a-zA-Z0-9]/", "", $c->shortname);
  102   return $shortname;
  103 }
  104 
  105 
  106 
  107 function wwassignment_add_instance($wwassignment) {
  108 /// Given an object containing all the necessary data,
  109 /// (defined by the form in mod.html) this function
  110 /// will create a new instance and return the id number
  111 /// of the new instance.
  112   $record = get_record("wwassignment_bridge", "course",$wwassignment->course);
  113   if (! $record ) {  #add bridge entry
  114     // check if bridge exists
  115     $wwassignment_bridge->timemodified = time();
  116         $wwassignment_bridge->course = $wwassignment->course;
  117     $wwassignment_bridge->coursename = _wwassignment_courseIdToShortName($wwassignment->course);
  118     $wwassignment_bridge->name = $wwassignment_bridge->coursename;
  119     if (! isset($wwassignment_bridge->coursename) ){
  120        $wwassignment_bridge->coursename = "foo";
  121     }
  122 //    error_log("add entry to wwmoodle");
  123 //    error_log(print_r($wwassignment, true));
  124     $returnid = insert_record("wwassignment_bridge",$wwassignment_bridge);
  125     error_log("inserting new entry to wwassignment_bridge");
  126   }
  127   # create set
  128   $aSetInfo = _wwrpc_getSetInfo($wwassignment->set_id, wwassignment_courseIdToShortName($wwassignment->course));
  129 
  130   $wwassignment->timemodified = time();
  131   $wwassignment->id = $wwassignment->instance;
  132   $wwassignment->timedue = $aSetInfo['due_date'];
  133   $wwassignment->timeavailable = $aSetInfo['open_date'];
  134   error_log("add wwassignment record");
  135 //  error_log(print_r($wwassignment, true));
  136   if ($returnid = insert_record("wwassignment", $wwassignment)) {
  137     if ($wwassignment->timedue) {
  138       $event = NULL;
  139       $event->name        = $wwassignment->name;
  140       $event->description = $wwassignment->description;
  141       $event->courseid    = $wwassignment->course;
  142       $event->groupid     = 0;
  143       $event->userid      = 0;
  144       $event->modulename  = 'wwassignment';
  145       $event->instance    = $returnid;
  146       $event->eventtype   = 'due';
  147       $event->timestart   = $wwassignment->timedue;
  148       $event->timeduration = 0;
  149       if (! add_event($event) ){
  150         error_log("wwassignment event not created when adding instance!");
  151       }
  152     }
  153   }
  154 
  155   return $returnid;
  156 }
  157 
  158 
  159 function wwassignment_update_instance($wwassignment) {
  160 /// Given an object containing all the necessary data,
  161 /// (defined by the form in mod.html) this function
  162 /// will update an existing instance with new data.
  163     $aSetInfo = _wwrpc_getSetInfo($wwassignment->set_id, wwassignment_courseIdToShortName($wwassignment->course));
  164 
  165   $wwassignment->timemodified = time();
  166   $wwassignment->id = $wwassignment->instance;
  167   $wwassignment->timedue = $aSetInfo['due_date'];
  168   $wwassignment->timeavailable = $aSetInfo['open_date'];
  169 
  170     error_log("updating wwassignment record");
  171     error_log(print_r($wwassignment, true));
  172   if ($returnid = update_record("wwassignment", $wwassignment)) {
  173     if ($wwassignment->timedue) {
  174       $event = NULL;
  175 
  176       if ($event->id = get_field('event', 'id', 'modulename', 'wwassignment', 'instance', $wwassignment->id)) {
  177 
  178         $event->name        = $wwassignment->name;
  179         $event->description = $wwassignment->description;
  180         $event->timestart   = $wwassignment->timedue;
  181 
  182         $rs = update_event($event) ;
  183 
  184         error_log("updating the event".$rs);
  185         error_log(print_r($event,true));
  186 
  187       } else {
  188         $event = NULL;
  189         $event->name        = $wwassignment->name;
  190         $event->description = $wwassignment->description;
  191         $event->courseid    = $wwassignment->course;
  192         $event->groupid     = 0;
  193         $event->userid      = 0;
  194         $event->modulename  = 'wwassignment';
  195         $event->instance    = $wwassignment->id;
  196         $event->eventtype   = 'due';
  197         $event->timestart   = $wwassignment->timedue;
  198         $event->timeduration = 0;
  199         add_event($event);
  200       }
  201     } else {
  202       delete_records('event', 'modulename', 'wwassignment', 'instance', $wwassignment->id);
  203     }
  204     return $returnid;
  205   }
  206 
  207 }
  208 function wwassignment_delete_instance($id) {
  209 /// Given an ID of an instance of this module,
  210 /// this function will permanently delete the instance
  211 /// and any data that depends on it.
  212     $result = true;
  213   if (! $wwassignment = get_record("wwassignment", "id", "$id")) {
  214     return false;
  215   }
  216 
  217   # Delete any dependent records here #
  218 
  219   if (! delete_records("wwassignment", "id", $wwassignment->id)) {
  220     $result = false;
  221   }
  222 
  223   if (! delete_records('event', 'modulename', 'wwassignment', 'instance', $wwassignment->id)) {
  224     $result = false;
  225   }
  226 
  227   // Get the cm id to properly clean up the grade_items for this assignment
  228   // bug 4976
  229   if (! $cm = get_record('modules', 'name', 'wwassignment')) {
  230     $result = false;
  231   } else {
  232     if (! delete_records('grade_item', 'modid', $cm->id, 'cminstance', $wwassignment->id)) {
  233       $result = false;
  234     }
  235   }
  236 
  237   return $result;
  238 }
  239 
  240 function wwassignment_user_outline($course, $user, $mod, $wwassignment) {
  241 /// Return a small object with summary information about what a
  242 /// user has done with a given particular instance of this module
  243 /// Used for user activity reports.
  244 /// $return->time = the time they did it
  245 /// $return->info = a short text description
  246   $aLogs = get_logs("l.userid=$user AND l.course=$course AND l.cmid={$wwassignment->id}");
  247   if( count($aLogs) > 0 ) {
  248     $return->time = $aLogs[0]->time;
  249     $return->info = $aLogs[0]->info;
  250   }
  251   return $return;
  252 }
  253 
  254 function wwassignment_user_complete($course, $user, $mod, $wwassignment) {
  255 /// Print a detailed representation of what a  user has done with
  256 /// a given particular instance of this module, for user activity reports.
  257 
  258   return true;
  259 }
  260 
  261 function wwassignment_print_recent_activity($course, $isteacher, $timestart) {
  262 /// Given a course and a time, this module should find recent activity
  263 /// that has occurred in wwassignment activities and print it out.
  264 /// Return true if there was output, or false is there was none.
  265 
  266     global $CFG;
  267 
  268     return false;  //  True if anything was printed, otherwise false
  269 }
  270 
  271 function wwassignment_cron () {
  272 /// Function to be run periodically according to the moodle cron
  273 /// This function searches for things that need to be done, such
  274 /// as sending out mail, toggling flags etc ...
  275 
  276     global $CFG;
  277 
  278     return true;
  279 }
  280 
  281 function wwassignment_grades($wwassignmentid) {
  282 /// Must return an array of grades for a given instance of this module,
  283 /// indexed by user.  It also returns a maximum allowed grade.
  284 ///
  285 ///    $return->grades = array of grades;
  286 ///    $return->maxgrade = maximum allowed grade;
  287 ///
  288 ///    return $return;
  289   // here's how we compute the grade:
  290   // NOTE: each set has P problems in it.
  291   // NOTE: each problem may be attempted M times.
  292   // NOTE: each problem has been attempted A times (by a given user).
  293   // NOTE: each problem was gotten correct C times (by a given user).
  294   // NOTE: each problem was gotten incorrect I times (by a given user).
  295   // Thus, a users grade is: sigma(over P) C/A
  296   // And the max score is P
  297   // Alternately, code is provided for:
  298   // sigma(over P) { if( C > 0) 1 else 0 }
  299   // again with a max of P
  300 
  301   // redefine it here, 'cause for some reason we can't global it...
  302     //debug_log("start grades ".$wwassignmentid);
  303   $wwassignment_gradeMethods = wwassignment_gradeMethods();
  304 
  305   $oGrades->grades = array();
  306   $aGrades->maxgrade = 0;
  307 
  308   $oMod = get_record("wwassignment", "id", $wwassignmentid);
  309   if( ! $oMod ) {
  310     return NULL;
  311   }
  312   //debug_log("record ".print_r($oMod,true));
  313   $gradeFormula = $wwassignment_gradeMethods[$oMod->gradingmethod]['formula'];
  314   //debug_log("formula ".print_r($gradeFormula, true));
  315   if( empty($gradeFormula) ) {
  316     return NULL;
  317   }
  318 
  319   $sCourseName = wwassignment_courseIdToShortName($oMod->course);
  320 
  321   // enumerate over the students in the course:
  322   $aStudents = get_course_students($oMod->course);
  323   foreach( $aStudents as $s ) {
  324     $aProblems = _wwrpc_getProblemsForUser($s->username, $oMod->set_id, $sCourseName);
  325     $fGrade = 0.0;
  326     foreach( $aProblems as $p ) {
  327       eval($gradeFormula);
  328     }
  329     $oGrades->grades[$s->id] = $fGrade;
  330   }
  331   $oGrades->maxgrade = _wwrpc_getMaxSetGrade($oMod->set_id, $sCourseName);
  332   return $oGrades;
  333 }
  334 
  335 function wwassignment_get_participants($wwassignmentid) {
  336 //Must return an array of user records (all data) who are participants
  337 //for a given instance of wwassignment. Must include every user involved
  338 //in the instance, independient of his role (student, teacher, admin...)
  339 //See other modules as example.
  340   $oMod = get_record("wwassignment", "id", $wwassignmentid);
  341   if( ! $oMod ) {
  342     return array();
  343   }
  344   return get_course_users($oMod->course);
  345 }
  346 
  347 function wwassignment_scale_used ($wwassignmentid,$scaleid) {
  348 //This function returns if a scale is being used by one wwassignment
  349 //it it has support for grading and scales. Commented code should be
  350 //modified if necessary. See forum, glossary or journal modules
  351 //as reference.
  352 
  353   $return = false;
  354 
  355   $rec = get_record("wwassignment","id","$wwassignmentid","scale","-$scaleid");
  356 
  357   if (!empty($rec)  && !empty($scaleid)) {
  358     $return = true;
  359   }
  360   return $return;
  361 }
  362 function wwassignment_refresh_events($courseid = 0) {
  363     error_log("wwassignment_refresh_events called");
  364     if ($courseid == 0) {
  365         if (! $wwassignment = get_records("wwassignment")) {
  366             return true;
  367         }
  368     } else {
  369         if (! $wwassignment = get_records("wwassignment", "course", $courseid)) {
  370             return true;
  371         }
  372     }
  373     $moduleid = get_field('modules', 'id', 'name', 'wwassignment');
  374 
  375     foreach ($wwassignment as $wwassignment) {
  376         $event = NULL;
  377         $event->name        = addslashes($wwassignment->name);
  378         $event->description = addslashes($wwassignment->description);
  379         $event->timestart   = $wwassignment->timedue;
  380 
  381         if ($event->id = get_field('event', 'id', 'modulename', 'wwassignment', 'instance', $wwassignment->id)) {
  382             update_event($event);
  383 
  384         } else {
  385             $event->courseid    = $wwassignment->course;
  386             $event->groupid     = 0;
  387             $event->userid      = 0;
  388             $event->modulename  = 'wwassignment';
  389             $event->instance    = $wwassignment->id;
  390             $event->eventtype   = 'due';
  391             $event->timeduration = 0;
  392             $event->visible     = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $wwassignment->id);
  393             add_event($event);
  394         }
  395 
  396     }
  397     return true;
  398 }
  399 
  400 
  401 ////////////////////////////////////////////////////////////////////////////////////
  402 // calls to WeBWorK database start with _wwrpc_
  403 ///////////////////////////////////////////////////////////////////////////////////
  404 
  405 /**
  406  * Gets information about the specified set.
  407  * @param int $iSetId The id of the set.
  408  * @param $sCourseName The name of this course
  409  * @return array Information about the set.
  410  */
  411 function _wwrpc_getSetInfo($iSetId, $sCourseName) {
  412   global $db, $CFG;
  413   $qry = "SELECT * FROM ". WW_TABLE_PREFIX.".{$sCourseName}_set WHERE set_id=?";
  414     //error_log("get info for set $iSetID and $sCourseName");
  415   if (!$res = $db->query($qry, array($iSetId))) {
  416         if (isset($CFG->debug) and $CFG->debug > 7) {
  417             notify($db->ErrorMsg() .'<br /><br />'. $sql);
  418         }
  419         if (!empty($CFG->dblogerror)) {
  420             $debug=array_shift(debug_backtrace());
  421             error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT:  $sql");
  422         }
  423         return false;
  424     }
  425   $row = $res->fetchRow();
  426   if( ! $row ) {
  427     return array('set_id' => $iSetId, 'set_header' => "Unable to get information for this set.", 'hardcopy_header' => "Unable to get information for this set.", 'open_date'=>time(), 'due_date'=>time(), 'answer_date'=>time(), 'published'=>time());
  428   }
  429   $res->free();
  430   //error_log("result from getSetInfo");
  431   return $row;
  432 }
  433 
  434 /**
  435  * Gets the max grade for a given problem set.
  436  * Essentially, this is just the number of problems in the set.
  437  * @param int $iSetId The id of the set.
  438  * @param string $sCourseName The name of this course.
  439  * @return int
  440  */
  441 function _wwrpc_getMaxSetGrade($iSetId, $sCourseName) {
  442   global $db, $CFG;
  443     if (!$res = $db->Execute($sql)) {
  444         if (isset($CFG->debug) and $CFG->debug > 7) {
  445             notify($db->ErrorMsg() .'<br /><br />'. $sql);
  446         }
  447         if (!empty($CFG->dblogerror)) {
  448             $debug=array_shift(debug_backtrace());
  449             error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT:  $sql");
  450         }
  451         return false;
  452     }
  453 
  454   $qry = "SELECT COUNT(*) FROM ". WW_TABLE_PREFIX.".{$sCourseName}_problem WHERE set_id=?";
  455   if (!$res = $db->query($qry, array($iSetId))) {
  456         if (isset($CFG->debug) and $CFG->debug > 7) {
  457             notify($db->ErrorMsg() .'<br /><br />'. $sql);
  458         }
  459         if (!empty($CFG->dblogerror)) {
  460             $debug=array_shift(debug_backtrace());
  461             error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT:  $sql");
  462         }
  463         return false;
  464     }
  465   $row = $res->fetchRow();
  466   $res->free();
  467   return $row ? $row['COUNT(*)'] : -1;
  468 }
  469 
  470 /**
  471  * Gets the results of all the problems in the given set for the given user.
  472  * @param string $sUserName The name of the user to check for.
  473  * @param int $iSetId The id of the set to check for.
  474  * @param string $sCourseName The name of this course.
  475  * @return array An array of the results of all the problems for this user.
  476  */
  477 function _wwrpc_getProblemsForUser($sUserName, $iSetId, $sCourseName) {
  478   // debug_log("start getProblemsForUser");
  479     global $db, $CFG;
  480   $qry = "SELECT * FROM ". WW_TABLE_PREFIX.".{$sCourseName}_problem_user WHERE user_id=? AND set_id=? ORDER BY problem_id";
  481   if (!$res = $db->query($qry, array($sUserName, $iSetId))) {
  482         if (isset($CFG->debug) and $CFG->debug > 7) {
  483             notify($db->ErrorMsg() .'<br /><br />'. $sql);
  484         }
  485         if (!empty($CFG->dblogerror)) {
  486             $debug=array_shift(debug_backtrace());
  487             error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT:  $sql");
  488         }
  489         return false;
  490     }
  491   $row = $res->fetchRow();
  492   !$row ? $row = array() : $row = $row;
  493   $res->free();
  494   return $row;
  495 }
  496 ////////////////////////////////////////////////////////////////////////////////////
  497 // internal functions start with _wwassignment
  498 ///////////////////////////////////////////////////////////////////////////////////
  499 
  500 /**
  501  * Prints an HTML select widget allowing for selection of any of the sets defined
  502  * for this course in WeBWorK.
  503  * @pre There is a WeBWorK course for this course.
  504  * @param int $iCourseId The id of this course.
  505  * @param int $iSetId The set id to have selected.
  506  * @return void
  507  */
  508 function _wwassignment_printSetSelect($iCourseId, $iSetId=-1) {
  509   global $db, $CFG;
  510   // debug_log("starting printSetSelect");
  511   $sCourseName = wwassignment_courseIdToShortName($iCourseId);
  512   if( is_null($sCourseName) ) {
  513     print("<b>Unable to find the name of this course.</b>\n");
  514     return;
  515   }
  516 
  517   // now get a list of all sets for this course:
  518   $qry = "SELECT * FROM ". WW_TABLE_PREFIX.".{$sCourseName}_set ORDER BY open_date DESC";
  519   if (!$res = $db->query($qry)) {
  520         if (isset($CFG->debug) and $CFG->debug > 7) {
  521             notify($db->ErrorMsg() .'<br /><br />'. $sql);
  522         }
  523         if (!empty($CFG->dblogerror)) {
  524             $debug=array_shift(debug_backtrace());
  525             error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT:  $sql");
  526         }
  527         return false;
  528     }
  529   $aSets = array();
  530   // debug_log("got sets");
  531   while( $row = $res->fetchRow() ) {
  532     $aSets[] = $row['set_id'];
  533   }
  534   $res->free();
  535 
  536 
  537   // now print the option box, if we have any to print:
  538   if( count($aSets) < 1 ) {
  539     print("<b>No sets exist for this course. Please create one via <a href='" . WWASSIGNMENT_WEBWORK_URL . "/$sCourseName'>WeBWorK</a>.</b>\n");
  540     return;
  541   }
  542   print("<select id='set_id' name='set_id'>\n");
  543   foreach( $aSets as $s ) {
  544     print("<option value='");
  545     p($s);
  546     print("'");
  547     if( $s == $iSetId ) {
  548       print("selected='selected'");
  549     }
  550     print(">$s</option>\n");
  551   }
  552   print("</select>\n");
  553 }
  554 
  555 /**
  556  * Maps a course ID number to it's (sanatized) shortname.
  557  * @param int $iCourseID The ID of the course.
  558  * @return string The shortname of the course, with unsafe characters removed. If the courseID is not found, null is returned.
  559  */
  560 function _wwassignment_courseIdToShortName($iCourseId) {
  561   $c = get_record('course', 'id', $iCourseId);
  562   if( ! $c ) {
  563     return null;
  564   }
  565   $shortname = preg_replace("/[^a-zA-Z0-9]/", "", $c->shortname);
  566   return $shortname;
  567 }
  568 
  569 
  570 /**
  571  * Returns a URL to the specified set.
  572  * @param int $iSetId The set ID to link to.
  573  * @param string $sCourseName The name of this course.
  574  * @return string The URL to the specified set. This might be absolute, or relative. However, it is assured of working.
  575  */
  576 function _wwassignment_linkToSet($iSetId, $sCourseName) {
  577   // TODO: Verify me.
  578   return WWASSIGNMENT_WEBWORK_URL."/$sCourseName/$iSetId";
  579 }
  580 ?>

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9