[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 5183 - (download) (as text) (annotate)
Wed Jul 18 17:58:06 2007 UTC (12 years, 7 months ago) by mleventi
File size: 30716 byte(s)
Changes to allow the new wwlink block to establish course link
Added README

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9