[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 5036 - (download) (as text) (annotate)
Tue Jun 26 06:46:33 2007 UTC (12 years, 8 months ago) by mleventi
File size: 31452 byte(s)
Fixed add_user functionality.

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9