[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 5109 - (download) (as text) (annotate)
Sun Jul 1 20:44:13 2007 UTC (12 years, 7 months ago) by mleventi
File size: 29959 byte(s)
Refactoring of some comments, Added Language Files, Quote Optimizations

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9