[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 5065 - (download) (as text) (annotate)
Thu Jun 28 21:32:25 2007 UTC (12 years, 7 months ago) by mleventi
File size: 32054 byte(s)
More bug squashed.

    1 <?php
    2 // $Id: lib.php,v 1.14 2007-06-28 21:32:25 mleventi Exp $
    3 
    4 require_once("$CFG->libdir/soap/nusoap.php");
    5 
    6 define('WWASSIGNMENT_WEBWORK_URL', $CFG->wwassignment_webworkurl);
    7 define('WWASSIGNMENT_WEBWORK_WSDL', $CFG->wwassignment_rpc_wsdl);
    8 define('WWASSIGNMENT_WEBWORK_KEY',$CFG->wwassignment_rpc_key);
    9 
   10 /**
   11 * @desc Creates an auto_login link to the URL of the webwork problem set pointed to by wwassignmentid for the current user. (will force set creation if necessary)
   12 * @param string $wwassignmentid The Moodle ID of the wwassignment.
   13 * @return URL to the assignment.
   14 */
   15 function wwassignment_view_link($wwassignmentid) {
   16     global $COURSE,$USER;
   17 
   18     $webworkclient =& new webwork_client();
   19     $webworkcourse = _wwassignment_mapped_course($COURSE->id,false);
   20     $webworkset = _wwassignment_mapped_set($wwassignmentid,false);
   21     $webworkuser = $webworkclient->mapped_user($webworkcourse,$USER->username);
   22     if($webworkuser == -1) {
   23         error_log(get_string("user_not_mapped","wwassignment"));
   24         $tempuser = $USER;
   25         $newuser = $webworkclient->create_user($webworkcourse,$tempuser);
   26         $webworkuser = $webworkclient->mapped_user($webworkcourse,$USER->username,false);
   27     }
   28 
   29     $webworksetuser = $webworkclient->mapped_user_set($webworkcourse,$webworkuser,$webworkset);
   30     if($webworksetuser == -1) {
   31         error_log(get_string("set_user_not_mapped","wwassignment"));
   32         //try and do it now
   33         $newsetuser = $webworkclient->create_user_set($webworkcourse,$webworkuser,$webworkset);
   34         //AGAIN FOR GOOD MEASURE
   35         $webworksetuser = $webworkclient->mapped_user_set($webworkcourse,$webworkuser,$webworkset,false);
   36     }
   37 
   38     $key = $webworkclient->login_user($webworkcourse,$webworkuser,false);
   39 
   40     return _wwassignment_link_to_set_auto_login($webworkcourse,$webworkset,$webworkuser,$key);
   41 }
   42 
   43 /**
   44 * @desc Creates an auto_login link to the URL for editing the problem set associated with $wwassignmentid.
   45 * @param string $wwassignmentid The assignment id.
   46 * @return URL The URL.
   47 */
   48 function wwassignment_edit_set_link($wwassignmentid) {
   49     global $COURSE,$USER;
   50     $webworkclient =& new webwork_client();
   51 
   52     //IS THE COURSE MAPPED?
   53     $webworkcourse = _wwassignment_mapped_course($COURSE->id,false);
   54     //IS THE SET MAPPED?
   55     $webworkset = _wwassignment_mapped_set($wwassignmentid,false);
   56     //IS THE USER MAPPED?
   57     $webworkuser = $webworkclient->mapped_user($webworkcourse,$USER->username);
   58     if($webworkuser == -1) {
   59         //USER WAS NOT FOUND, wasnt mapped
   60         error_log(get_string("user_not_mapped","wwassignment"));
   61         //try and create a teacher
   62         $tempuser = $USER;
   63         $newuser = $webworkclient->create_user($webworkcourse,$tempuser,"10");
   64         //AGAIN FOR GOOD MEASURE
   65         $webworkuser = $webworkclient->mapped_user($webworkcourse,$USER->username,false);
   66     }
   67     $key = $webworkclient->login_user($webworkcourse,$webworkuser,false);
   68     return _wwassignment_link_to_edit_set_auto_login($webworkcourse,$webworkset,$webworkuser,$key);
   69 }
   70 
   71 /**
   72 * @desc Creates an auto_login link to the URL of the instructor page for the webwork course associated with the current course.
   73 * @return URL The URL.
   74 */
   75 function wwassignment_instructor_page_link() {
   76     global $COURSE,$USER;
   77     $webworkclient =& new webwork_client();
   78 
   79     $webworkcourse = _wwassignment_mapped_course($COURSE->id,false);
   80     //IS THE USER MAPPED?
   81     $webworkuser = $webworkclient->mapped_user($webworkcourse,$USER->username);
   82     if($webworkuser == -1) {
   83         //USER WAS NOT FOUND, wasnt mapped
   84         error_log(get_string("user_not_mapped","wwassignment"));
   85         //try and create a teacher
   86         $tempuser = $USER;
   87         $newuser = $webworkclient->create_user($webworkcourse,$tempuser,"10");
   88         //AGAIN FOR GOOD MEASURE
   89         $webworkuser = $webworkclient->mapped_user($webworkcourse,$USER->username,false);
   90     }
   91     $key = $webworkclient->login_user($webworkcourse,$webworkuser,false);
   92 
   93     return _wwassignment_link_to_instructor_auto_login($webworkcourse,$webworkuser,$key);
   94 }
   95 
   96 /**
   97 * @desc On the first instance in a course this will add the Moodle/WeBWorK tie. Otherwise it will create a new Moodle assignment <-> Webwork problem set tie.
   98 */
   99 function wwassignment_add_instance($wwassignment) {
  100     global $COURSE,$SESSION,$USER;
  101 
  102 
  103     if(isset($wwassignment->webwork_course)) {
  104         //Attaching Moodle Course to WeBWorK Course
  105         if($wwassignment->webwork_course =="create") {
  106             print_error("Creation is not yet supported.");
  107         }
  108         error_log("adding wwassignment_bridge record");
  109         $wwassignment->course = $COURSE->id;
  110         $returnid = insert_record("wwassignment_bridge",$wwassignment);
  111         $wwassignment->webwork_set = "undefined";
  112         $returnid = insert_record("wwassignment",$wwassignment);
  113         $webworkclient =& new webwork_client();
  114         if($webworkclient->mapped_user($wwassignment->webwork_course,$USER->username) == -1) {
  115             $webworkclient->create_user($wwassignment->webwork_course,$USER,"10");
  116         }
  117         return $returnid;
  118     }
  119     if(isset($wwassignment->webwork_set)) {
  120         $webworkclient =& new webwork_client();
  121         if($wwassignment->webwork_set =="create") {
  122             print_error("Creation is not yet supported.");
  123         }
  124         $webworkcourse = _wwassignment_mapped_course($COURSE->id);
  125         $webworksetdata = $webworkclient->get_assignment_data($webworkcourse,$wwassignment->webwork_set,false);
  126         //Attaching Moodle Set to WeBWorK Set
  127         error_log("adding wwassignment record");
  128         $wwassignment->course = $COURSE->id;
  129         $wwassignment->name = get_string("set_name","wwassignment") . " " . $webworksetdata['set_id'];
  130         $returnid = insert_record("wwassignment",$wwassignment);
  131         //GET TIMEDUE INFO AND ADD AN EVENT
  132 
  133         if(isset($webworksetdata)) {
  134             $event = NULL;
  135             $event->name = get_string("set_name","wwassignment") . " " . $webworksetdata['set_id'];
  136             $event->description = "";
  137             $event->courseid = $COURSE->id;
  138             $event->groupid = 0;
  139             $event->userod = 0;
  140             $event->modulename = 'wwassignment';
  141             $event->instance = $returnid;
  142             $event->eventtype = 'due';
  143             $event->timestart = $webworksetdata['due_date'];
  144             $event->timeduration = 0;
  145             if(!add_event($event)) {
  146                 error_log(get_string("event_creation_error","wwassignment"));
  147             }
  148         }
  149     }
  150     return $returnid;
  151 }
  152 
  153 /**
  154 * @desc Updates and resynchronizes all information related to the a moodle assignment <-> webwork problem set tie.
  155 */
  156 function wwassignment_update_instance($wwassignment) {
  157     global $COURSE;
  158     $webworkclient =& new webwork_client();
  159     $webworkcourse = _wwassignment_mapped_course($COURSE->id);
  160     $webworkset = _wwassignment_mapped_set($wwassignment->instance,false);
  161     $webworksetdata = $webworkclient->get_assignment_data($webworkcourse,$webworkset,false);
  162     if(isset($webworksetdata)) {
  163         $wwassignment->name = get_string("set_name","wwassignment") . " " . $webworksetdata['set_id'];
  164         if($returnid = update_record("wwassignment",$wwassignment)) {
  165             $event = NULL;
  166             if($event->id = get_field('event','id','modulename','wwassignment','instance',$wwassignment->id)) {
  167                 $event->name = get_string("set_name","wwassignment") . " " . $webworksetdata['set_id'];
  168                 $event->timestart = $webworksetdata['due_date'];
  169                 $rs = update_event($event);
  170                 error_log("updating the event".$rs);
  171             } else {
  172                 $event = NULL;
  173                 $event->name        = get_string("set_name","wwassignment") . " " . $webworksetdata['set_id'];
  174                 $event->description = "";
  175                 $event->courseid    = $COURSE->id;
  176                 $event->groupid     = 0;
  177                 $event->userid      = 0;
  178                 $event->modulename  = 'wwassignment';
  179                 $event->instance    = $wwassignment->id;
  180                 $event->eventtype   = 'due';
  181                 $event->timestart   = $webworksetdata['due_date'];
  182                 $event->timeduration = 0;
  183                 add_event($event);
  184             }
  185         }
  186 
  187     }
  188     return $returnid;
  189 }
  190 
  191 /**
  192 * @desc Deletes a tie in Moodle. Deletes nothing in webwork.
  193 * @param integer $id The id of the assignment to delete.
  194 */
  195 function wwassignment_delete_instance($id) {
  196 
  197     $result = true;
  198     if (! $wwassignment = get_record("wwassignment", "id", "$id")) {
  199         return false;
  200     }
  201 
  202     # Delete any dependent records here #
  203 
  204     if (! delete_records("wwassignment", "id", $wwassignment->id)) {
  205         $result = false;
  206     }
  207 
  208     if (! delete_records('event', 'modulename', 'wwassignment', 'instance', $wwassignment->id)) {
  209         $result = false;
  210     }
  211 
  212     // Get the cm id to properly clean up the grade_items for this assignment
  213     // bug 4976
  214     if (! $cm = get_record('modules', 'name', 'wwassignment')) {
  215         $result = false;
  216     } else {
  217         if (! delete_records('grade_item', 'modid', $cm->id, 'cminstance', $wwassignment->id)) {
  218             $result = false;
  219         }
  220     }
  221 
  222     return $result;
  223 }
  224 
  225 function wwassignment_user_outline($course, $user, $mod, $wwassignment) {
  226 /// Return a small object with summary information about what a
  227 /// user has done with a given particular instance of this module
  228 /// Used for user activity reports.
  229 /// $return->time = the time they did it
  230 /// $return->info = a short text description
  231     $aLogs = get_logs("l.userid=$user AND l.course=$course AND l.cmid={$wwassignment->id}");
  232     if( count($aLogs) > 0 ) {
  233         $return->time = $aLogs[0]->time;
  234         $return->info = $aLogs[0]->info;
  235     }
  236     return $return;
  237 }
  238 
  239 function wwassignment_user_complete($course, $user, $mod, $wwassignment) {
  240 /// Print a detailed representation of what a  user has done with
  241 /// a given particular instance of this module, for user activity reports.
  242 
  243     return true;
  244 }
  245 
  246 function wwassignment_print_recent_activity($course, $isteacher, $timestart) {
  247 /// Given a course and a time, this module should find recent activity
  248 /// that has occurred in wwassignment activities and print it out.
  249 /// Return true if there was output, or false is there was none.
  250 
  251         global $CFG;
  252 
  253         return false;  //  True if anything was printed, otherwise false
  254 }
  255 
  256 function wwassignment_cron () {
  257 /// Function to be run periodically according to the moodle cron
  258 /// This function searches for things that need to be done, such
  259 /// as sending out mail, toggling flags etc ...
  260 
  261         global $CFG;
  262 
  263         return true;
  264 }
  265 /**
  266 * @desc Contacts webwork to find out the completion status of a problem set for all users in a course.
  267 * @param integer $wwassignmentid The problem set
  268 */
  269 function wwassignment_grades($wwassignmentid) {
  270 /// Must return an array of grades for a given instance of this module,
  271 /// indexed by user.  It also returns a maximum allowed grade.
  272 ///
  273 ///    $return->grades = array of grades;
  274 ///    $return->maxgrade = maximum allowed grade;
  275 ///
  276 ///    return $return;
  277     // here's how we compute the grade:
  278     // NOTE: each set has P problems in it.
  279     // NOTE: each problem may be attempted M times.
  280     // NOTE: each problem has been attempted A times (by a given user).
  281     // NOTE: each problem was gotten correct C times (by a given user).
  282     // NOTE: each problem was gotten incorrect I times (by a given user).
  283     // Thus, a users grade is: sigma(over P) C/A
  284     // And the max score is P
  285     // Alternately, code is provided for:
  286     // sigma(over P) { if( C > 0) 1 else 0 }
  287     // again with a max of P
  288 
  289     // redefine it here, 'cause for some reason we can't global it...
  290     //debug_log("start grades ".$wwassignmentid);
  291     global $COURSE;
  292     $webworkclient =& new webwork_client();
  293     $studentgrades->grades = array();
  294     $studentgrades->maxgrade = 0;
  295         //debug_log("record ".print_r($oMod,true));
  296     $gradeformula = '$finalgrade += ($problem->status > 0) ? 1 : 0;';
  297     //debug_log("formula ".print_r($gradeFormula, true));
  298     $webworkcourse = _wwassignment_mapped_course($COURSE->id,false);
  299     $webworkset = _wwassignment_mapped_set($wwassignmentid,false);
  300 
  301     // enumerate over the students in the course:
  302     $students = get_course_students($COURSE->id);
  303 
  304      foreach( $students as $student ) {
  305         $webworkuser = $webworkclient->mapped_user($webworkcourse,$student->username);
  306         if($webworkuser != -1) {
  307             $webworkuserset = $webworkclient->mapped_user_set($webworkcourse,$webworkuser,$webworkset);
  308             if($webworkuserset != -1)
  309             {
  310                 $studentproblems = $webworkclient->get_user_problems($webworkcourse,$webworkuser,$webworkset);
  311                 $finalgrade = 0.0;
  312                 foreach( $studentproblems as $problem ) {
  313                     $finalgrade += $problem->status;
  314                 }
  315             } else {
  316                 $finalgrade = 0.0;
  317             }
  318         } else {
  319             $finalgrade = 0.0;
  320         }
  321         $studentgrades->grades[$student->id] = $finalgrade;
  322      }
  323 
  324     $studentgrades->maxgrade = $webworkclient->get_max_grade($webworkcourse,$webworkset);
  325     //error_log("all grades".print_r($studentgrades, true));
  326 
  327     return $studentgrades;
  328 }
  329 
  330 /**
  331 * @desc Finds all the participants in the course
  332 * @param string $wwassignmentid The Moodle wwassignment ID.
  333 * @return array An array of course users (IDs).
  334 */
  335 function wwassignment_get_participants($wwassignmentid) {
  336     $wwassignment = get_record("wwassignment", "id", $wwassignmentid);
  337     if(!isset($wwassignment)) {
  338         return array();
  339     }
  340     return get_course_users($wwassignment->course);
  341 }
  342 
  343 function wwassignment_refresh_events($courseid = 0) {
  344     error_log("wwassignment_refresh_events called");
  345     if ($courseid == 0) {
  346         if (! $wwassignment = get_records("wwassignment")) {
  347             return true;
  348         }
  349     } else {
  350         if (! $wwassignment = get_records("wwassignment", "course", $courseid)) {
  351             return true;
  352         }
  353     }
  354     $moduleid = get_field('modules', 'id', 'name', 'wwassignment');
  355 
  356     foreach ($wwassignment as $wwassignment) {
  357         $event = NULL;
  358         $event->name        = addslashes($wwassignment->name);
  359         $event->description = addslashes($wwassignment->description);
  360         $event->timestart   = $wwassignment->timedue;
  361 
  362         if ($event->id = get_field('event', 'id', 'modulename', 'wwassignment', 'instance', $wwassignment->id)) {
  363             update_event($event);
  364 
  365         } else {
  366             $event->courseid    = $wwassignment->course;
  367             $event->groupid     = 0;
  368             $event->userid      = 0;
  369             $event->modulename  = 'wwassignment';
  370             $event->instance    = $wwassignment->id;
  371             $event->eventtype   = 'due';
  372             $event->timeduration = 0;
  373             $event->visible     = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $wwassignment->id);
  374             add_event($event);
  375         }
  376 
  377     }
  378     return true;
  379 }
  380 
  381 
  382 ////////////////////////////////////////////////////////////////////////////////////
  383 // internal functions start with _wwassignment
  384 ///////////////////////////////////////////////////////////////////////////////////
  385 
  386 
  387 ////////////////////////////////////////////////////////////////
  388 //functions that check mapping existance in the local db
  389 ////////////////////////////////////////////////////////////////
  390 
  391 /**
  392 * @desc Finds the webwork course name from a moodle course id.
  393 * @param integer $courseid Moodle Course ID.
  394 * @param integer $silent whether to trigger an error message.
  395 * @return string the name of the webwork course on success and -1 on failure.
  396 */
  397 function _wwassignment_mapped_course($courseid,$silent = true) {
  398     $wwassignmentbridge = get_record('wwassignment_bridge','course', $courseid);
  399     if((isset($wwassignmentbridge)) && (isset($wwassignmentbridge->webwork_course))) {
  400         return $wwassignmentbridge->webwork_course;
  401     }
  402     if(!$silent) {
  403         print_error(get_string("webwork_map_failure","wwassignment") . " No WeBWorK course is mapped for this course.");
  404     }
  405     return -1;
  406 }
  407 
  408 /**
  409 * @desc Finds the webwork set name from a wwassignment id.
  410 * @param integer $wwassignmentid Moodle wwassignment ID.
  411 * @param integer $silent whether to trigger an error message.
  412 * @return string the name of the webwork set on success and -1 on failure.
  413 */
  414 function _wwassignment_mapped_set($wwassignmentid,$silent = true) {
  415     $wwassignment = get_record('wwassignment','id',$wwassignmentid);
  416     if((isset($wwassignment)) && (isset($wwassignment->webwork_set))) {
  417         return $wwassignment->webwork_set;
  418     }
  419     if(!$silent) {
  420         print_error(get_string("webwork_map_failure","wwassignment") . " No WeBWorK set is mapped for this set.");
  421     }
  422     return -1;
  423 }
  424 
  425 ////////////////////////////////////////////////////////////////
  426 //functions that create links to the webwork site.
  427 ////////////////////////////////////////////////////////////////
  428 
  429 /**
  430 * @desc Returns URL link to a webwork course logging the user in.
  431 * @param string $webworkcourse The webwork course.
  432 * @param string $webworkset The webwork set.
  433 * @param string $webworkuser The webwork user.
  434 * @param string $key The key used to login the user.
  435 * @return URL.
  436 */
  437 function _wwassignment_link_to_edit_set_auto_login($webworkcourse,$webworkset,$username,$key) {
  438     return _wwassignment_link_to_course($webworkcourse) . "instructor/sets/$webworkset/?effectiveUser=$username&user=$username&key=$key";
  439 }
  440 
  441 
  442 /**
  443 * @desc Returns URL link to a webwork course logging the user in.
  444 * @param string $webworkcourse The webwork course.
  445 * @param string $webworkuser The webwork user.
  446 * @param string $key The key used to login the user.
  447 * @return URL.
  448 */
  449 function _wwassignment_link_to_instructor_auto_login($webworkcourse,$username,$key) {
  450     return _wwassignment_link_to_course($webworkcourse) . "instructor/?effectiveUser=$username&user=$username&key=$key";
  451 }
  452 
  453 /**
  454 * @desc Returns the URL link to a webwork course and a particular set logged in.
  455 * @param string $webworkcourse The webwork course.
  456 * @param string $webworkset The webwork set.
  457 * @param string $webworkuser The webwork user.
  458 * @param string $key The key used to login the user.
  459 * @return URL.
  460 */
  461 function _wwassignment_link_to_set_auto_login($webworkcourse,$webworkset,$webworkuser,$key) {
  462     return _wwassignment_link_to_set($webworkcourse,$webworkset) .  "?effectiveUser=$webworkuser&user=$webworkuser&key=$key";
  463 }
  464 
  465 /**
  466 * @desc Returns the URL link to a webwork course and a particular set.
  467 * @param string $webworkcourse The webwork course.
  468 * @param string $webworkset The webwork set.
  469 * @return URL.
  470 */
  471 function _wwassignment_link_to_set($webworkcourse,$webworkset) {
  472     return _wwassignment_link_to_course($webworkcourse) . "$webworkset/";
  473 }
  474 
  475 /**
  476 * @desc Returns the URL link to a webwork course.
  477 * @param string $webworkcourse The webwork course.
  478 * @return URL.
  479 */
  480 function _wwassignment_link_to_course($webworkcourse) {
  481     return WWASSIGNMENT_WEBWORK_URL."/$webworkcourse/";
  482 }
  483 
  484 ///////////////////////////////////////////////////////////////
  485 //webwork client class
  486 ///////////////////////////////////////////////////////////////
  487 
  488 /**
  489 * @desc This singleton class acts as the gateway for all communication from the Moodle Client to the WeBWorK SOAP Server. It encapsulates an instance of a SoapClient.
  490 */
  491 class webwork_client {
  492         var $client;
  493         var $defaultparams;
  494         var $datacache;
  495         var $mappingcache;
  496         /**
  497          * @desc Constructs a singleton webwork_client.
  498          */
  499         function webwork_client()
  500         {
  501             // static associative array containing the real objects, key is classname
  502             static $instances=array();
  503             // get classname
  504             $class = get_class($this);
  505             if (!array_key_exists($class, $instances)) {
  506                 // does not yet exist, save in array
  507                 $this->client = new soap_client(WWASSIGNMENT_WEBWORK_WSDL,'wsdl');
  508                 $err = $this->client->getError();
  509                 if ($err) {
  510                     print_error("Constructor Error");
  511                 }
  512                 $this->defaultparams = array();
  513                 $this->defaultparams['authenKey']  = WWASSIGNMENT_WEBWORK_KEY;
  514                 $this->datacache = array();
  515                 $this->mappingcache = array();
  516                 $instances[$class] = $this;
  517 
  518             }
  519             foreach (get_class_vars($class) as $var => $value) {
  520                 $this->$var =& $instances[$class]->$var;
  521             }
  522         }
  523         /**
  524          *@desc Calls a SOAP function and passes (authenkey,course) automatically in the parameter list.
  525          *@param string $functioncall The function to call
  526          *@param array $params The parameters to the function.
  527          *@param integer $override=false whether to override the default parameters that are passed to the soap function (authenKey).
  528          *@return Result of the soap function.
  529          */
  530         function handler($functioncall,$params=array(),$override=false) {
  531                 if(!is_array($params)) {
  532                         $params = array($params);
  533                 }
  534                 if(!$override) {
  535                         $params = array_merge($this->defaultparams,$params);
  536                 }
  537                 $result = $this->client->call($functioncall,$params);
  538                 //$result = call_user_func_array(array(&$this->client,$functioncall),$params);
  539                 if($err = $this->client->getError()) {
  540                         print_error(get_string("rpc_fault","wwassignment") . " " . $functioncall. " ". $err);
  541                 }
  542                 return $result;
  543         }
  544 
  545         /**
  546         * @desc Checks whether a user is in a webwork course.
  547         * @param string $webworkcourse The webwork course name.
  548         * @param string $webworkuser The webwork user name.
  549         * @param integer $silent whether to trigger an error message
  550         * @return string Returns the webwork user on success and -1 on failure.
  551         */
  552         function mapped_user($webworkcourse,$webworkuser,$silent = true) {
  553             if(isset($this->mappingcache[$webworkcourse]['user'][$webworkuser])) {
  554                 return $this->mappingcache[$webworkcourse]['user'][$webworkuser];
  555             }
  556             $record = $this->handler('get_user',array("courseName" => $webworkcourse,"userID" => $webworkuser));
  557             if($record != -1) {
  558                 $this->mappingcache[$webworkcourse]['user'][$webworkuser] = $webworkuser;
  559                 return $webworkuser;
  560             }
  561             if(!$silent) {
  562                 print_error(get_string("webwork_map_failure","wwassignment") . "The user $webworkuser was not found in the webwork course $webworkcourse.");
  563             }
  564             return -1;
  565         }
  566 
  567         /**
  568         * @desc Checks whether a user has his own copy of a set built in a webwork course.
  569         * @param string $webworkcourse The webwork course name.
  570         * @param string $webworkuser The webwork user name.
  571         * @param string $webworkset The webwork set name.
  572         * @param integer $silent whether to trigger an error message
  573         * @return integer Returns 1 on success and -1 on failure.
  574         */
  575         function mapped_user_set($webworkcourse,$webworkuser,$webworkset,$silent = true) {
  576             if(isset($this->mappingcache[$webworkcourse]['user_set'][$webworkuser][$webworkset])) {
  577                 return $this->mappingcache[$webworkcourse]['user_set'][$webworkuser][$webworkset];
  578             }
  579             $record = $this->handler("get_user_set",array("courseName" => $webworkcourse,"userID" => $webworkuser,"setID" => $webworkset));
  580             if($record != -1) {
  581                 $this->mappingcache[$webworkcourse]['user_set'][$webworkuser][$webworkset] = 1;
  582                 return 1;
  583             }
  584 
  585             if(!$silent) {
  586                 print_error(get_string("webwork_map_failure","wwassignment") . "The user $webworkuser does not have copies of the set $webworkset in webwork course $webworkcourse.");
  587             }
  588             return -1;
  589         }
  590 
  591         /**
  592         * @desc Gets the record of the global set for a webwork course and set name.
  593         * @param string $webworkcourse The webwork course name.
  594         * @param string $webworkset The webwork set name.
  595         * @param integer $silent whether to trigger an error message
  596         * @return array Returns set information on success or -1 on failure.
  597         */
  598         function get_assignment_data($webworkcourse,$webworkset,$silent = true) {
  599             $record = $this->handler("get_global_set",array('courseName' => $webworkcourse, 'setID' => $webworkset));
  600             if(isset($record)) {
  601                 $setinfo = array();
  602                 $setinfo['open_date'] = $record->open_date;
  603                 $setinfo['due_date'] = $record->due_date;
  604                 $setinfo['set_id'] = $record->set_id;
  605                 $setinfo['name'] = get_string("set_name","wwassignment") . " " . $record->set_id;
  606                 return $setinfo;
  607             }
  608             if(!$silent) {
  609                 print_error(get_string("webwork_map_failure","wwassignment") . "The set $webworkset was not found  in webwork course $webworkcourse.");
  610             }
  611             return -1;
  612 
  613         }
  614 
  615         /**
  616         * @desc Gets all the user problems for a specfic course, user and set.
  617         * @param string $webworkcourse The webwork course name.
  618         * @param string $webworkuser The webwork users name.
  619         * @param string $webworkset The webwork set name.
  620         * @param integer $silent whether to trigger an error message
  621         * @return array Returns an array of problems on success or -1 on failure.
  622         */
  623         function get_user_problems($webworkcourse,$webworkuser,$webworkset,$silent = true) {
  624             $record = $this->handler("get_all_user_problems",array("courseName" => $webworkcourse,"userID" => $webworkuser,"setID" => $webworkset));
  625             if(isset($record)) {
  626                 return $record;
  627             }
  628             if(!$silent) {
  629                 print_error(get_string("webwork_map_failure","wwassignment") . "The user $webworkuser does not have copies of the set $webworkset in webwork course $webworkcourse.");
  630             }
  631             return -1;
  632         }
  633 
  634         /**
  635         * @desc Calculates the max grade on a set by counting the number of problems in the set.
  636         * @param string $webworkcourse The webwork course name.
  637         * @param string $webworkset The webwork set name.
  638         * @param integer $silent whether to trigger an error message
  639         * @return integer The max grade on success or -1 on failure.
  640         */
  641         function get_max_grade($webworkcourse,$webworkset,$silent = true) {
  642             $record = $this->handler('list_global_problems',array("courseName" => $webworkcourse,"setID" => $webworkset));
  643             if(isset($record)) {
  644                 return count($record);
  645             }
  646             if(!$silent) {
  647                 print_error(get_string("webwork_map_failure","wwassignment") . "The set $webworkset does not exist in webwork course $webworkcourse.");
  648             }
  649             return -1;
  650 
  651         }
  652         /**
  653         * @desc Forces a login of a user into a course.
  654         * @param string $webworkcourse The webwork course name.
  655         * @param string $webworkuser The webwork users name.
  656         * @param integer $silent whether to trigger an error message
  657         * @return string The webwork key for URL on success or -1 on failure.
  658         */
  659         function login_user($webworkcourse,$webworkuser,$silent = true) {
  660             $key = $this->handler("login_user",array("courseName" => $webworkcourse,"userID" => $webworkuser));
  661             if(isset($key)) {
  662                 return $key;
  663             }
  664             if(!$silent) {
  665                 print_error(get_string("webwork_map_failure","wwassignment") . "The user $webworkuser cannot login to $webworkcourse.");
  666             }
  667             return -1;
  668         }
  669 
  670         /**
  671         * @desc Retrieves a list of sets from a webwork course and converts it into form options format.
  672         * @param string $webworkcourse The webwork course name.
  673         * @param integer $silent whether to trigger an error message
  674         * @return array The form options.
  675         */
  676         function options_set($webworkcourse,$silent = true) {
  677             $setlist = $this->handler('list_global_sets',array("courseName" => $webworkcourse));
  678             if(isset($setlist)) {
  679                 $setoptions = array();
  680                 foreach($setlist as $setid) {
  681                     $setoptions[$setid] = $setid;
  682                 }
  683                 return $setoptions;
  684             }
  685             if(!$silent) {
  686                 print_error(get_string("webwork_map_failure","wwassignment") . "Could not get sets from the course $webworkcourse.");
  687             }
  688             return -1;
  689         }
  690 
  691         /**
  692         * @desc Retrieves a list of courses from a webwork course and converts it into form options format.
  693         * @param integer $silent whether to trigger an error message
  694         * @return array The form options.
  695         */
  696         function options_course($silent = true) {
  697             $courselist = $this->handler("list_courses");
  698             if(isset($courselist)) {
  699                 $courseoptions = array();
  700                 foreach($courselist as $course) {
  701                     $courseoptions[$course] = $course;
  702                 }
  703                 return $courseoptions;
  704             }
  705             if(!$silent) {
  706                 print_error(get_string("webwork_map_failure","wwassignment") . "Could not get list of courses.");
  707             }
  708             return -1;
  709 
  710         }
  711 
  712         /**
  713         * @desc Creates a user in the WeBWorK course.
  714         * @param string $webworkcourse The webwork course name.
  715         * @param array $userdata The user data to use in creation.
  716         * @param string $permission The permissions of the new user, defaults to 0.
  717         * @return Returns 1 on success.
  718         */
  719         function create_user($webworkcourse,&$userdata,$permission="0") {
  720             $studentid = $userid;
  721             $this->handler("add_user",array("courseName" => $webworkcourse, "record" => array(
  722                 "user_id" => $userdata->username,
  723                 "first_name" => $userdata->firstname,
  724                 "last_name" => $userdata->lastname,
  725                 "email_address" => $userdata->emailaddress,
  726                 "student_id" => $studentid,
  727                 "status" => "C",
  728                 "section" => "",
  729                 "recitation" => "",
  730                 "comment" => "moodle created user")));
  731             $this->handler("add_permission",array("courseName" => $webworkcourse,"record" => array(
  732                 "user_id" => $userdata->username,
  733                 "permission" => $permission)));
  734             $this->handler("add_password",array("courseName" => $webworkcourse,"record" => array(
  735                 "user_id" => $userdata->username,
  736                 "password" => $studentid)));
  737             return 1;
  738         }
  739 
  740         /**
  741         * @desc Creates a user set in WeBWorK
  742         * @param string $webworkcourse The webwork course name.
  743         * @param string $webworkuser The webwork user name.
  744         * @param string $webworkset The webwork set name.
  745         * @return Returns 1 on success.
  746         */
  747         function create_user_set($webworkcourse,$webworkuser,$webworkset) {
  748             $this->handler("assign_set_to_user",array("courseName" => $webworkcourse,"userID" => $webworkuser, "setID" => $webworkset));
  749             return 1;
  750         }
  751 };
  752 
  753 ?>

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9