From: Mike G. v. a. <we...@ma...> - 2008-05-22 14:52:28
|
Log Message: ----------- wwassignment4 is a version of "bridge1" which is compatible with moodle 1.9 and webwork 2.4.x It adds a new assignment like activity called wwassignment. It works but does not yet have much testing and has the potential that the webwork and moodle databases might get out of sync. The structure is similar enough that it might eventually be a good idea to implement it as a new assignment type. Bridge2 adds a question type to the moodle quiz. It is requires a different set of software Added Files: ----------- wwmoodle/wwassignment4: CHANGES README wwmoodle/wwassignment4/bin/setup: setup.pl wwmoodle/wwassignment4/moodle/blocks/wwlink: block_wwlink.php config_instance.html wwmoodle/wwassignment4/moodle/lang/en_utf8: block_wwlink.php wwassignment.php wwmoodle/wwassignment4/moodle/mod/wwassignment: backuplib.php config.html icon.gif index.php javascript.php lib.php locallib.php mod_form.php restorelib.php test.php version.php view.php wwmoodle/wwassignment4/moodle/mod/wwassignment/db: install.xml upgrade.php wwmoodle/wwassignment4/moodle/mod/wwassignment/lang: wwassignment.php wwmoodle/wwassignment4/moodle/mod/wwassignment/simpletest: testwwassignment.php Revision Data ------------- --- /dev/null +++ wwassignment4/bin/setup/setup.pl @@ -0,0 +1,105 @@ +#!/usr/bin/env perl + +use Cwd; + +sub promptUser { + + #-------------------------------------------------------------------# + # two possible input arguments - $promptString, and $defaultValue # + # make the input arguments local variables. # + #-------------------------------------------------------------------# + + local($promptString,$defaultValue) = @_; + + #-------------------------------------------------------------------# + # if there is a default value, use the first print statement; if # + # no default is provided, print the second string. # + #-------------------------------------------------------------------# + + if ($defaultValue) { + print $promptString, "[", $defaultValue, "]: "; + } else { + print $promptString, ": "; + } + + $| = 1; # force a flush after our print + $_ = <STDIN>; # get the input from STDIN (presumably the keyboard) + + + #------------------------------------------------------------------# + # remove the newline character from the end of the input the user # + # gave us. # + #------------------------------------------------------------------# + + chomp; + + #-----------------------------------------------------------------# + # if we had a $default value, and the user gave us input, then # + # return the input; if we had a default, and they gave us no # + # no input, return the $defaultValue. # + # # + # if we did not have a default value, then just return whatever # + # the user gave us. if they just hit the <enter> key, # + # the calling routine will have to deal with that. # + #-----------------------------------------------------------------# + + if ("$defaultValue") { + return $_ ? $_ : $defaultValue; # return $_ if it has a value + } else { + return $_; + } +} + +print "###################################\n"; +print "#WeBWorK Assignment in Moodle #\n"; +print "###################################\n"; + +#Continue? +print "This script will setup the WeBWorK Assignment Type.\n"; +$continue = promptUser('Continue','y'); +if($continue ne "y") { + exit; +} + +#Program Root +print "Please enter the root directory where wwassignment3 module is located. \n"; +print "Example: /tmp/wwmoodle/wwassignment3\n"; +$wwassignmentRoot = promptUser(''); + +#Moodle Root +print "Please enter the root directory where Moodle is installed. \n"; +print "Example: /var/www/moodle \n"; +$moodleRoot = promptUser(''); + +#File Moving/Linking +$files = promptUser('Would you like me to place the files into proper directories (y,n)','y'); +if($files eq 'y') { + $doWhat = promptUser('Would you like me to copy the help files or soft link them.(copy,link)','link'); + if($doWhat eq 'link') { + $action = 'ln -sf '; + } elsif ($doWhat eq 'copy') { + print "Remember to rerun setup when/if you update from the CVS\n"; + $action = 'cp -R '; + } else { + exit; + } + #wipe existing directories + system("rm -rf $moodleRoot/mod/wwassignment"); + system("rm -rf $moodleRoot/blocks/wwlink"); + system("rm -rf $moodleRoot/lang/en_utf8/help/wwassignment"); + + #copy php code directories + system("cp -R $wwassignmentRoot/moodle/mod/wwassignment " . $moodleRoot . '/mod/'); + system("cp -R $wwassignmentRoot/moodle/blocks/wwlink " . $moodleRoot . '/blocks/'); + + #help files + system($action . "$wwassignmentRoot/moodle/lang/en_utf8/wwassignment.php " . $moodleRoot . '/lang/en_utf8/wwassignment.php'); + system($action . "$wwassignmentRoot/moodle/lang/en_utf8/block_wwlink.php " . $moodleRoot . '/lang/en_utf8/block_wwlink.php'); + system($action . "$wwassignmentRoot/moodle/lang/en_utf8/help/wwassignment " . $moodleRoot . '/lang/en_utf8/help/'); + + print "Setup Successful!\n"; + + +} + +1; --- /dev/null +++ wwassignment4/moodle/blocks/wwlink/config_instance.html @@ -0,0 +1,36 @@ +<?php + global $CFG; + $lib = $CFG->dirroot . '/mod/wwassignment/locallib.php'; +?> +<table cellpadding="9" cellspacing="0" align="center"> + <?php if(!file_exists($lib)) { ?> + <tr valign="top"> + <td align="center"> + <?php echo '<strong>'.get_string('config_wwassigment_mod_missing','block_wwlink').'</strong>'; ?> + </td> + </tr> + <?php } else { ?> + <tr valign="top"> + + <td align="right"> + <?php print_string('config_select_webwork_course', 'block_wwlink'); ?> + </td> + <td align="center"> + <?php + require_once($lib); + $client = new wwassignment_client(); + $options = $client->options_course(true); + choose_from_menu($options,'webwork_link_id',empty($this->config->webwork_link_id) ? 0 : $this->config->webwork_link_id); + global $COURSE; + $courseid = $COURSE->id; + ?> + <input type="hidden" name="courseid" value="<?php echo $courseid;?>" /> + </td> + </tr> + <tr> + <td colspan="2" align="center"> + <input type="submit" value="<?php print_string('savechanges') ?>" /> + </td> + </tr> + <?php } ?> +</table> --- /dev/null +++ wwassignment4/moodle/blocks/wwlink/block_wwlink.php @@ -0,0 +1,71 @@ +<?php + +/** +* @desc This block creates the tie between a Moodle course and a WeBWorK course +*/ +class block_wwlink extends block_base { + + /** + * @desc Sets the title of the block and the current version. + */ + function init() { + $this->title = get_string('blockname','block_wwlink'); + $this->version = 2007092100; + } + /** + * @desc Allows each instance of the block to have its own configuration settings. + */ + function instance_allow_config() { + return true; + } + + /** + * @desc Saves the form data from configuration into the wwassignment_bridge table. + */ + function instance_config_save($data) { + + $webworkcourse = $data->webwork_link_id; + $moodlecourse = $data->courseid; + + $wwassignmentbridge = new stdClass; + $wwassignmentbridge->course = $moodlecourse; + $wwassignmentbridge->webwork_course = $webworkcourse; + + //has this mapping been defined + $record = get_record('wwassignment_bridge','course',$moodlecourse); + if(!$record) { + //new one + insert_record('wwassignment_bridge',$wwassignmentbridge); + } else { + //update + $wwassignmentbridge->id = $record->id; + update_record('wwassignment_bridge',$wwassignmentbridge); + } + + return parent::instance_config_save($data); + } + + /** + * @desc Makes sure that the only place this block can be added is on course-view page. This insures one block per course. + */ + function applicable_formats() { + return array('all' => false, 'course-view' => true); + } + + /** + * @desc Prints the content of the block. Whether or not the course is connected to a moodle course. + */ + function get_content() { + global $COURSE; + + $courseid = $COURSE->id; + $record = get_record('wwassignment_bridge','course',$courseid); + if(!$record) { + $this->content->text = get_string('not_connected','block_wwlink'); + } else { + $this->content->text = get_string('connected','block_wwlink') . ' ' . $record->webwork_course; + } + return $this->content; + } +} +?> --- /dev/null +++ wwassignment4/moodle/lang/en_utf8/wwassignment.php @@ -0,0 +1,61 @@ +<?php +// $Id: wwassignment.php,v 1.1 2008/05/22 14:36:24 gage Exp $ +// language strings for wwmoodle_set + +$string['modulename'] = "WeBWorK Problem Set"; +$string['modulenameplural'] = "WeBWorK Problem Sets"; + +$string['set_name'] = $string['modulename']; + +//index list +$string['open_date'] = "Opened"; +$string['due_date'] = "Due"; + +//viewing page +$string['go_to_webwork'] = "Administer the WeBWorK portion of this course."; +$string['iframeNoShow-1'] = "If you are unable to view this problem set, please "; +$string['iframeNoShow-2'] = "click here"; + +//Defining New Module without Block +$string['mainpage_link_desc'] = 'You need to create the webwork course mapping by creating a WeBWorK link block.'; +$string['mainpage_link_name'] = 'Course Page'; + +//Course Link form +$string['course_initialization'] = 'Linking to WeBWorK'; +$string['webwork_course'] = 'WeBWorK Course'; + +//Set Link form +$string['set_initialization'] = 'Linking to WeBWorK Problem Set'; +$string['wwassignmentname'] = 'Name'; +$string['webwork_set'] = 'WeBWorK Set'; +$string['instructor_page_link_desc'] = 'Link to the Instructor Section of WeBWorK'; +$string['instructor_page_link_name'] = 'Go To WeBWorK Instructor Tools'; + +$string['edit_set_link_desc'] = 'Link to Set Modification Section of WeBWorK'; +$string['edit_set_link_name'] = 'Modify Set'; + +//CONFIG +$string['webwork_url'] = "The URL location of webwork on the server. ex: http://www.example.org/webwork2"; +$string['iframe_width'] = "The width in percentage of the page of the iframe displaying WeBWorK problem sets."; +$string['iframe_height'] = "The height in pixels of the iframe displaying WeBWorK problem sets."; +$string['rpc_wsdl'] = "Path to the WSDL file on the host running WeBWorK."; +$string['rpc_key'] = "Key identifying Moodle to WeBWorK."; +$string['testchanges'] = "Test WeBWorK Connection."; + + +//ERRORS +$string['construction_error'] = 'An error occured in establishing the connection to WeBWorK.<br>The most likely cause of this problem is that your WSDL path is incorrect.'; + +$string['event_creation_error'] = 'wwassignment event could not be created.'; +$string['webwork_course_list_map_failure'] = 'Could not retrieve a list of courses on the current WeBWorK server.'; + +$string['webwork_course_map_failure'] = 'No WeBWorK course is linked to this course.<br>Use the WWLink block to create a connection between this Moodle course and a WeBWorK course.'; + +$string['webwork_set_map_failure'] = 'No WeBWorK problem set is mapped to the current Moodle problem set.'; +$string['webwork_user_map_failure'] = 'No WeBWorK user is mapped to the current Moodle user.'; +$string['webwork_user_set_map_failure'] = 'The WeBWorK user does not have the current Moodle set assigned.'; +$string['rpc_error'] = 'Communication error between the Moodle client and WeBWorK server.'; + + + +?> \ No newline at end of file --- /dev/null +++ wwassignment4/moodle/lang/en_utf8/block_wwlink.php @@ -0,0 +1,11 @@ +<?php + +$string['blockname'] = 'WeBWorK Link'; +$string['webworklink'] = 'WeBWorK Link'; + +$string['config_select_webwork_course'] = 'Select WeBWorK Course'; + +$string['connected'] = 'Connected to WeBWorK Course:'; +$string['not_connected'] = 'Not Connected to WeBWorK'; + +?> --- /dev/null +++ wwassignment4/moodle/mod/wwassignment/index.php @@ -0,0 +1,95 @@ +<?php +/** +* @desc This page lists all the instances of wwassignments within a particular course. +*/ + +require_once("../../config.php"); +require_once("locallib.php"); + +//get the course ID from GET line +$id = required_param('id', PARAM_INT); + +//check this course exists +if (!$course = get_record("course", "id", $id)) { + error("Course ID is incorrect"); +} + +//force login +require_login($course->id); +add_to_log($course->id, "wwassignment", "view all", "index.php?id=$course->id", ""); + +//Get all required strings +$strwwassignments = get_string("modulenameplural", "wwassignment"); +$strwwassignment = get_string("modulename", "wwassignment"); + +//Print the header +if ($course->category) { + $navigation = "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> »"; +} +print_header("$course->shortname: $strwwassignments", "$course->fullname", "$navigation $strwwassignments", "", "", true, "", navmenu($course)); + +//Get all the appropriate data +if (!$wwassignments = get_all_instances_in_course("wwassignment", $course)) { + notice("There are no $strwwassignments", "../../course/view.php?id=$course->id"); + die; +} + +//Print the list of instances (your module will probably extend this) +$timenow = time(); +$strname = get_string("name"); +$strweek = get_string("week"); +$strtopic = get_string("topic"); +$strdescription = get_string('description'); +$stropendate = get_string("open_date", "wwassignment"); +$strduedate = get_string("due_date", "wwassignment"); + +if ($course->format == "weeks") { + $table->head = array ($strweek, $strname,$strdescription, $stropendate, $strduedate); + $table->align = array ("center", "left", "left", "left", "left"); +} else if ($course->format == "topics") { + $table->head = array ($strtopic, $strname,$strdescription, $stropendate, $strduedate); + $table->align = array ("center", "left", "left", "left", "left", "left", "left"); +} else { + $table->head = array ($strname,$strdescription, $stropendate, $strduedate); + $table->align = array ("left", "left", "left", "left", "left", "left"); +} + +$wwclient = new wwassignment_client(); +$wwcoursename = _wwassignment_mapped_course($COURSE->id,false); + +foreach ($wwassignments as $wwassignment) { + // grab specific info for this set: + if(isset($wwassignment)) { + $wwsetname = $wwassignment->webwork_set; + $wwsetinfo = $wwclient->get_assignment_data($wwcoursename,$wwsetname,false); + + if (!$wwassignment->visible) { + //Show dimmed if the mod is hidden + $link = "<a class=\"dimmed\" href=\"view.php?id=$wwassignment->coursemodule\">$wwassignment->name</a>"; + } else { + //Show normal if the mod is visible + $link = "<a href=\"view.php?id=$wwassignment->coursemodule\">$wwassignment->name</a>"; + } + if ($course->format == "weeks" or $course->format == "topics") { + $table->data[] = array ($wwassignment->section, $link, $wwassignment->description, strftime("%c", $wwsetinfo['open_date']), strftime("%c", $wwsetinfo['due_date'])); + } else { + $table->data[] = array ($link, $wwassignment->description, strftime("%c", $wwsetinfo['open_date']), strftime("%c", $wwsetinfo['due_date'])); + } + } +} + +echo "<br />"; + +print_table($table); + +/*if( isteacher($course->id) ) { + $wwusername = $USER->username; + + $wwlink = _wwassignment_link_to_instructor_auto_login($wwcoursename,$wwusername,) + print("<p style='font-size: smaller; color: #aaa; text-align: center;'><a style='color: #666;text-decoration:underline' href='".WWASSIGNMENT_WEBWORK_URL."/$course->shortname/instructor' target='_webwork_edit'>".get_string("go_to_webwork", "wwassignment")."</a></p>"); +}*/ + +/// Finish the page +print_footer($course); + +?> --- /dev/null +++ wwassignment4/moodle/mod/wwassignment/lib.php @@ -0,0 +1,524 @@ +<?php +global $CFG; +require_once("locallib.php"); +// debug switch defined in locallib.php define('WWASSIGNMENT_DEBUG',0); + +//////////////////////////////////////////////////////////////// +// External grade triggers +// wwassignment_update_grades(wwassignment,userid=0) is called from +// grade_update_mod_grades in gradlib.php and also from wwassignment/upgrade.php file +// grade_update_mod_grades is called by $grade_item->refresh_grades +// +// wwassignment_grade_item_update(wwassignment) +// is called from grade_update_mod_grades (before update_grades(wwassignment,userid=0))) +// +// wwassignment_get_user_grades +// could be called from wwassignment/index.php pages (legacy??) +// +// Internal grade calling structure +// +// wwassignment_update_grades($wwassignment=null, $userid=0, $nullifnone=true) -- updates grades for assignment instance or all instances +// wwassignment_get_user_grades($wwassignment,$userid=0) -- fetches homework grades from WeBWorK +// _wwassignment_get_course_students($courseid) -- collects users from moodle database +// $wwclient->grade_users_sets($webworkcourse,$webworkusers,$webworkset) -- fetches grades from a given course, set and user collection +// wwassignment_grade_item_update(wwassignment, grades) +// grade_update(...) -- fills record in grade_item table and possibly in grade_grades table as well +// +// wwassignment_update_grade_item(wwassignment) -- just updates grade_item table +// wwassignment_update_grade_item(wwassignment, grades) updates grade_item table and grade_grades table +//////////////////////////////////////////////////////////////// + + + +//////////////////////////////////////////////////////////////// +//Functions that are called by the Moodle System +//////////////////////////////////////////////////////////////// + +/** +* @desc Called when the module is installed into the Moodle System +*/ +function wwassignment_install() { +} + +/** +* @desc Creates a new Moodle assignment <-> Webwork problem set tie. +* @param $wwassignment object The data of the record to be entered in the DB. +* @return integer The ID of the new record. +*/ +function wwassignment_add_instance($wwassignment) { + global $COURSE; + + debugLog("Begin wwassignment_add_instance"); + debugLog("input wwassignment "); + //debugLot( print_r($wwassignment, true) ); + + //Get data about the set from WebWorK + $wwclient = new wwassignment_client(); + $wwcoursename = _wwassignment_mapped_course($COURSE->id,false); + $wwsetname = $wwassignment->webwork_set; + $wwsetdata = $wwclient->get_assignment_data($wwcoursename,$wwsetname,false); + + + + //Attaching Moodle Set to WeBWorK Set + //debugLog("saving wwassignment "); + //debugLog( print_r($wwassignment,true)); + + $wwassignment->timemodified = time(); + if ($returnid = insert_record("wwassignment", $wwassignment)) { + $wwassignment->id = $returnid; + + //Creating events + _wwassignment_create_events($wwsetname,$wwassignment->id,$wwsetdata['open_date'],$wwsetdata['due_date']); + + //notify gradebook + wwassignment_grade_item_update($wwassignment); + } + debugLog("End wwassignment_add_instance"); + return $returnid; +} + +/** +* @desc Updates and resynchronizes all information related to the a moodle assignment <-> webwork problem set tie. +* except for grades +* @param $wwassignment object The data of the record to be updated in the DB. +* @return integer The result of the update_record function. +*/ +function wwassignment_update_instance($wwassignment) { + global $COURSE; + require_once("locallib.php"); + debugLog("Begin wwassignment_update_instance"); + + + //checking mappings + $wwclient = new wwassignment_client(); + $wwcoursename = _wwassignment_mapped_course($COURSE->id,false); + $wwsetname = $wwassignment->webwork_set; + + //get data from WeBWorK + $wwsetdata = $wwclient->get_assignment_data($wwcoursename,$wwsetname,false); + $wwassignment->id = $wwassignment->instance; + + $wwassignment->timemodified = time(); + $returnid = update_record('wwassignment',$wwassignment); + + _wwassignment_delete_events($wwassignment->id); + _wwassignment_create_events($wwsetname,$wwassignment->id,$wwsetdata['open_date'],$wwsetdata['due_date']); + + //notify gradebook -- updates grade_item only + + wwassignment_grade_item_update($wwassignment); + debugLog("End wwassignment_update_instance"); + return $returnid; +} + +/** +* @desc Deletes a tie in Moodle. Deletes nothing in webwork. +* @param integer $wwassignmentid The id of the assignment to delete. +* @return bool Delete was successful or not. +*/ +function wwassignment_delete_instance($wwassignmentid) { + debugLog("Begin wwassignment_delete_instance -- input wwassignmentid:"); + debugLog(print_r($wwassignmentid,true)); + $result = true; + + #delete DB record + if ( ! $wwassignment = get_record('wwassignment', 'id',$wwassignmentid)) { + $result = false; + } + + $wwassignment->courseid = $wwassignment->course; + + #delete events + _wwassignment_delete_events($wwassignmentid); + + + // Get the cm id to properly clean up the grade_items for this assignment + // bug 4976 +// if (! $cm = get_record('modules', 'name', 'wwassignment')) { +// $result = false; +// } else { +// if (! delete_records('grade_item', 'modid', $cm->id, 'cminstance', $wwassignment->id)) { +// $result = false; +// } +// } + + if (! delete_records('wwassignment', 'id', $wwassignment->id)) { + $result = false; + } + + //notify gradebook + wwassignment_grade_item_delete($wwassignment); + debugLog("End wwassignment_delete_instance -- input wwassignmentid:"); + + return $result; +} + + /** gradebook upgrades + * add xxx_update_grades() function into mod/xxx/lib.php +Ê Ê * add xxx_grade_item_update() function into mod/xxx/lib.php +Ê Ê * patch xxx_update_instance(), xxx_insert_instance()? xxx_add_instance() and xxx_delete_instance() to call xxx_grade_item_update() +Ê Ê * patch all places of code that change grade values to call xxx_update_grades() +Ê Ê * patch code that displays grades to students to use final grades from the gradebookÊ + **/ + + +/** + * Return grade for given user or all users. + * + * @param int $assignmentid id of assignment + * @param int $userid optional user id, 0 means all users + * @return array array of grades, false if none + */ +// function assignment_get_user_grades($assignment, $userid=0) { +// global $CFG; +// +// $user = $userid ? "AND u.id = $userid" : ""; +// +// $sql = "SELECT u.id, u.id AS userid, s.grade AS rawgrade, s.submissioncomment AS feedback, s.format AS feedbackformat, +// s.teacher AS usermodified, s.timemarked AS dategraded, s.timemodified AS datesubmitted +// FROM {$CFG->prefix}user u, {$CFG->prefix}assignment_submissions s +// WHERE u.id = s.userid AND s.assignment = $assignment->id +// $user"; +// +// return get_records_sql($sql); +// } +// object returned looks like an array of standard objects +// ( +// [22] => stdClass Object +// ( +// [userid] => 22 +// [rawgrade] => -1 +// [feedback] => 23 +// [feedbackformat] => 0 +// [usermodified] => 2 +// [dategraded] => 1211200838 +// [datesubmitted] => 1211199392 +// [id] => 22 +// ) +// +// ) + +function wwassignment_get_user_grades($wwassignment,$userid=0) { + debugLog("Begin wwassignment_get_user_grades"); + //debugLog("inputs -- wwassignment" . print_r($wwassignment,true)); + //debugLog("userid = $userid"); + + require_once("locallib.php"); + + //checking mappings + $courseid = $wwassignment->course; + $wwclient = new wwassignment_client(); + $wwcoursename = _wwassignment_mapped_course($courseid,false); + $wwsetname = $wwassignment->webwork_set; + $usernamearray = array(); + $students = array(); + $studentgrades = array(); + if ($userid) { + $user = get_complete_user_data('id',$userid); + $username = $user->username; + array_push($usernamearray, $username); + array_push($students, $user); + } else { // get all student names + $students = _wwassignment_get_course_students( $courseid); + foreach($students as $student) { + array_push($usernamearray,$student->username); + } + } + + $gradearray = $wwclient->grade_users_sets($wwcoursename,$usernamearray,$wwsetname); // FIXME? return key/value pairs instead? + // returns an array of grades -- the number of questions answered correctly? + // debugLog("usernamearray " . print_r($usernamearray, true)); + // debugLog("grades($wwcoursename,usernamearray,$wwsetname) = " . print_r($gradearray, true)); + // model for output of grades + + $i =0; + foreach($students as $student) { + $studentid = $student->id; + $grade = new stdClass(); + $grade->userid = $studentid; + $grade->rawgrade =$gradearray[$i]; + $grade->feedback = "some text"; + $grade->feedbackformat = 0; + $grade->usermodified = 0; + $grade->dategraded = 0; + $grade->datesubmitted = 0; + $grade->id = $studentid; + $studentgrades[$studentid] = $grade; + $i++; + } + + + + // end model + //debugLog("output student grades:" . print_r($studentgrades,true) ); + debugLog("End wwassignment_get_user_grades"); + return $studentgrades; +} + +/** + * Update grades by firing grade_updated event + * + * @param object $assignment null means all wwassignments + * @param int $userid specific user only, 0 mean all + */ +function wwassignment_update_grades($wwassignment=null, $userid=0, $nullifnone=true) { + debugLog("Begin wwassignment_update_grades"); + //debugLog("inputs wwassignment = " . print_r($wwassignment,true)); + //debugLog("userid = $userid"); + global $CFG; + if (!function_exists('grade_update')) { //workaround for buggy PHP versions + require_once($CFG->libdir.'/gradelib.php'); + } + + if ($wwassignment != null) { + if ($grades = wwassignment_get_user_grades($wwassignment, $userid)) { # fetches all students if userid=0 + foreach($grades as $k=>$v) { + if ($v->rawgrade == -1) { + $grades[$k]->rawgrade = null; + } + } + debugLog("call wwassignment to update grade_item and record grades in gradebook "); + //debugLog(print_r($grades,true)); + wwassignment_grade_item_update($wwassignment, $grades); + } else { + wwassignment_grade_item_update($wwassignment); + } + + } else { // find all the assignments + debugLog("import grades for all wwassignments for all courses"); + $sql = "SELECT a.*, cm.idnumber as cmidnumber, a.course as courseid + FROM {$CFG->prefix}wwassignment a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m + WHERE m.name='wwassignment' AND m.id=cm.module AND cm.instance=a.id"; + + //$sql = " SELECT a.* FROM {$CFG->prefix}wwassignment a"; + //debugLog ("sql string = $sql"); + //$tmp = get_recordset_sql($sql); + //error_log("result is ".print_r($tmp,true) ); + if ($rs = get_recordset_sql($sql)) { + debugLog("record set found"); + while ($wwassignment = rs_fetch_next_record($rs)) { + debugLog("processing next grade"); + if ($wwassignment->grade != 0) { + wwassignment_update_grades($wwassignment); + } else { + wwassignment_grade_item_update($wwassignment); + } + } + rs_close($rs); + } + } + + debugLog("End wwassignment_update_grades"); + +} +/** + * Create grade item for given assignment + * + * @param object $wwassignment object with extra cmidnumber + * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook + * @return int 0 if ok, error code otherwise + */ +function wwassignment_grade_item_update ($wwassignment, $grades=NULL) { + $msg = "Begin wwassignment_grade_item_update"; + $msg = ($grades)? $msg . " with grades (updates grade_grades table)" :$msg; + debugLog($msg); + // debugLog("inputs wwassignment " . print_r($wwassignment, true)); + // debugLog("grades " . print_r($grades, true) ); + global $CFG; + if (!function_exists('grade_update')) { //workaround for buggy PHP versions + require_once($CFG->libdir.'/gradelib.php'); + } + + if (!isset($wwassignment->courseid)) { + $wwassignment->courseid = $wwassignment->course; + } + if (!isset($wwassignment->grade) ) { // this case occurs when the set link is edited from moodle activity editor + $wwclient = new wwassignment_client(); + $wwcoursename = _wwassignment_mapped_course($wwassignment->courseid,false); //last 'false' means report errors + $wwsetname = _wwassignment_mapped_set($wwassignment->id,false); + $wwassignment->grade = $wwclient->get_max_grade($wwcoursename,$wwsetname,false); + } + + // debugLog("wwassignment->grade set to ". $wwassignment->grade); + + $params = array('itemname'=>$wwassignment->name, 'idnumber'=>$wwassignment->cmidnumber); + + if ($wwassignment->grade > 0) { + $params['gradetype'] = GRADE_TYPE_VALUE; + $params['grademax'] = $wwassignment->grade; + $params['grademin'] = 0; + + } else if ($wwassignment->grade < 0) { + $params['gradetype'] = GRADE_TYPE_SCALE; + $params['scaleid'] = -$wwassignment->grade; + + } else { + $params['gradetype'] = GRADE_TYPE_TEXT; // allow text comments only + } + + if ($grades === 'reset') { + $params['reset'] = true; + $grades = NULL; + } + # grade_update() defined in gradelib.php + # $grades=NULL means update grade_item table only, otherwise post grades in grade_grades + debugLog("End wwassignment_grade_item_update"); + return grade_update('mod/wwassignment', $wwassignment->courseid, 'mod', 'wwassignment', $wwassignment->id, 0, $grades, $params); +} +/** + * Delete grade item for given assignment + * + * @param object $wwassignment object + * @return object wwassignment ???? + */ +function wwassignment_grade_item_delete($wwassignment) { + debugLog("Begin wwassignment_grade_item_delete"); + debugLog("inputs wwassignment " . print_r($wwassignment, true) ); + + global $CFG; + require_once($CFG->libdir.'/gradelib.php'); + + if (!isset($wwassignment->courseid)) { + $wwassignment->courseid = $wwassignment->course; + } + debugLog("End wwassignment_grade_item_delete"); + return grade_update('mod/wwassignment', $wwassignment->courseid, 'mod', 'wwassignment', $wwassignment->id, 0, NULL, array('deleted'=>1)); + + +} +/** + * Updates an assignment instance + * + * This is done by calling the update_instance() method of the assignment type class + */ +function wwassignment_item_update($wwassignment) { + error_log("Begin wwassignment_item_update -- not yet defined!!!!"); + error_log("input wwassignment " . print_r($wwassignment,true) ); + error_log("End wwassignment_item_update -- not yet defined!!!!"); +} +/** +* @desc Contacts webwork to find out the completion status of a problem set for all users in a course. +* @param integer $wwassignmentid The problem set +* @return object The student grades indexed by student ID. +*/ +function wwassignment_grades($wwassignmentid) { + error_log("Begin wwassignment_grades -- legacy function?"); + global $COURSE; + $wwclient = new wwassignment_client(); + $wwassignment = get_record('wwassignment', 'id',$wwassignmentid); + $courseid = $wwassignment->course; + + $studentgrades = new stdClass; + $studentgrades->grades = array(); + $studentgrades->maxgrade = 0; + + $gradeformula = '$finalgrade += ($problem->status > 0) ? 1 : 0;'; + + $wwcoursename = _wwassignment_mapped_course($courseid,false); + $wwsetname = _wwassignment_mapped_set($wwassignmentid,false); + + // enumerate over the students in the course: + $students = get_course_students( $courseid); + + $usernamearray = array(); + foreach($students as $student) { + array_push($usernamearray,$student->username); + } + $gradearray = $wwclient->grade_users_sets($wwcoursename,$usernamearray,$wwsetname); + $i = 0; + foreach($students as $student) { + $studentgrades->grades[$student->id] = $gradearray[$i]; + $i++; + } + $studentgrades->maxgrade = $wwclient->get_max_grade($wwcoursename,$wwsetname); + error_log("End wwassignment_grades -- legacy function?"); + return $studentgrades; +} + + +/** +* @desc Returns a small object with summary information about a wwassignment instance. Used for user activity repots. +* @param string $course The ID of the course. +* @param string $user The ID of the user. +* @param string $wwassignment The ID of the wwassignment instance. +* @return array Representing time, info pairing. +*/ +function wwassignment_user_outline($course, $user, $mod, $wwassignment) { + $aLogs = get_logs("l.userid=$user AND l.course=$course AND l.cmid={$wwassignment->id}"); + if( count($aLogs) > 0 ) { + $return->time = $aLogs[0]->time; + $return->info = $aLogs[0]->info; + } + return $return; +} + +/** +* @desc Prints a detailed representation of what a user has done with a instance of this module. +* @param string $course The ID of the course. +* @param string $user The ID of the user. +* @param string $wwassignment The ID of the wwassignment instance. +* @return array Representing time, info pairing. +*/ +function wwassignment_user_complete($course, $user, $mod, $wwassignment) { + return true; +} + + + +function wwassignment_delete_course() { +} + +function wwassignment_process_options() { +} + +function wwassignment_reset_course_form() { +} + +function wwassignment_delete_userdata() { + +} + +/** +* @desc Finds recent activity that has occured in wwassignment activities. +*/ +function wwassignment_print_recent_activity($course, $isteacher, $timestart) { + global $CFG; + return false; // True if anything was printed, otherwise false +} + +/** +* @desc Function that is run by the cron job. This makes sure that all data is pushed to webwork. +* returns true if successful +*/ +function wwassignment_cron() { + error_log("Begin wwassignment_cron --not used yet"); + + //FIXME: Add a call that updates all events with dates (in case people forgot to push) + error_log("End wwassignment_cron"); + return true; +} + + + +/** +* @desc Finds all the participants in the course +* @param string $wwassignmentid The Moodle wwassignment ID. +* @return array An array of course users (IDs). +*/ + +function wwassignment_get_participants($wwassignmentid) { + error_log("wwassignment_get_participants -- legacy??"); + $wwassignment = get_record('wwassignment', 'id', $wwassignmentid); + if(!isset($wwassignment)) { + return array(); + } + return _wwassignment_get_course_students( $wwassignment->course ); +} + +function wwassignment_refresh_events($courseid = 0) { + error_log('wwassignment_refresh_events called --not yet defined'); + return true; +} + +?> \ No newline at end of file --- /dev/null +++ wwassignment4/moodle/mod/wwassignment/config.html @@ -0,0 +1,56 @@ +<?php +// $Id: config.html,v 1.1 2008/05/22 14:36:24 gage Exp $ + + if( empty($CFG->wwassignment_webworkurl) ) { + $CFG->wwassignment_webworkurl = "HOSTNAME/webwork2"; + } + if( empty($CFG->wwassignment_iframewidth) ) { + $CFG->wwassignment_iframewidth = "95%"; + } + if( empty($CFG->wwassignment_iframeheight) ) { + $CFG->wwassignment_iframeheight = "500px"; + } + if( empty($CFG->wwassignment_rpc_wsdl) ) { + $CFG->wwassignment_rpc_wsdl = "HOSTNAME/webwork2_wsdl"; + } + if( empty($CFG->wwassignment_rpc_key) ) { + $CFG->wwassignment_rpc_key = ""; + } +?> +<form method="post" action="module.php" name="form"> + <input type="hidden" name="sesskey" value="<?php echo $USER->sesskey ?>"> + <table cellpadding="9" cellspacing="0"> + <tr valign='top'> + <td align="right"><label for='wwassignment_webworkurl'><b>wwassignment_webworkurl:</b></label></td> + <td><input type='text' name='wwassignment_webworkurl' id='wwassignment_webworkurl' value="<?php p($CFG->wwassignment_webworkurl); ?>" size="30" /></td> + <td><?php print_string('webwork_url', 'wwassignment'); ?></td> + </tr> + + <tr valign='top'> + <td align='right'><label for='wwassignment_iframewidth'><b>wwassignment_iframewidth:</b></label></td> + <td><input type='text' name='wwassignment_iframewidth' id='wwassignment_iframewidth' value="<?php p($CFG->wwassignment_iframewidth); ?>" size="30" /></td> + <td><?php print_string('iframe_width', 'wwassignment'); ?></td> + </tr> + <tr valign='top'> + <td align='right'><label for='wwassignment_iframeheight'><b>wwassignment_iframeheight:</b></label></td> + <td><input type='text' name='wwassignment_iframeheight' id='wwassignment_iframeheight' value="<?php p($CFG->wwassignment_iframeheight); ?>" size="30" /></td> + + <td><?php print_string('iframe_height', 'wwassignment'); ?></td> + </tr> + <tr valign='top'> + <td align='right'><label for='wwassignment_rpc_wsdl'><b>wwassignment_rpc_wsdl:</b></label></td> + <td><input type='text' name='wwassignment_rpc_wsdl' id='wwassignment_rpc_wsdl' value="<?php p($CFG->wwassignment_rpc_wsdl); ?>" size="30" /></td> + <td><?php print_string('rpc_wsdl', 'wwassignment'); ?></td> + </tr> + <tr valign='top'> + + <td align='right'><label for='wwassignment_rpc_key'><b>wwassignment_rpc_key:</b></label></td> + <td><input type='text' name='wwassignment_rpc_key' id='wwassignment_rpc_key' value="<?php p($CFG->wwassignment_rpc_key); ?>" size="30" /></td> + <td><?php print_string('rpc_key', 'wwassignment'); ?></td> + </tr> + <tr> + <td colspan="3" align="center"><input type="submit" value="<?php print_string("savechanges") ?>" /></td> + </tr> + </table> + +</form> --- /dev/null +++ wwassignment4/moodle/mod/wwassignment/version.php @@ -0,0 +1,12 @@ +<?php +// $Id: version.php,v 1.1 2008/05/22 14:36:24 gage Exp $ +///////////////////////////////////////////////////////////////////////////////// +/// Code fragment to define the version of wwassignment +/// This fragment is called by moodle_needs_upgrading() and /admin/index.php +///////////////////////////////////////////////////////////////////////////////// + +$module->version = 2008042072; // The current module version (Date: YYYYMMDDXX) +$module->requires = 2007101509; // Requires this Moodle version +$module->cron = 0; // Period for cron to check this module (secs) + +?> \ No newline at end of file --- /dev/null +++ wwassignment4/moodle/mod/wwassignment/test.php @@ -0,0 +1,22 @@ +<?php + require_once("/Library/WebServer/Documents/moodle//config.php"); +error_log("begin test"); + +global $COURSE; +$COURSE->id=7; +require_once("lib.php"); +require_once("locallib.php"); +error_log("test update_grades for assignment44 and user 41 mabernat"); +$wwassignment = get_record('wwassignment','id',44); //get a sample assignment id=44 +$wwassignment->cmidnumber =1; //How would we usually get this? +wwassignment_update_grades($wwassignment, 0); // +error_log("end test update_grades"); +// error_log("test ww_get_user_grades course 44 user 41"); +// $wwassignment = get_record('wwassignment','id',44); //get a sample assignment id=44 +// $userid =0; //user mabernat 41 user gage 2 user richman 20 +// wwassignment_get_user_grades($wwassignment,$userid); +// error_log("end test ww_get_user_grades"); +error_log("test completed"); + + +?> \ No newline at end of file --- /dev/null +++ wwassignment4/moodle/mod/wwassignment/mod_form.php @@ -0,0 +1,72 @@ +<?php +require_once ('moodleform_mod.php'); +require_once ('locallib.php'); + +class mod_wwassignment_mod_form extends moodleform_mod { + + function definition() { + global $COURSE,$USER; + $mform =& $this->_form; + + //Is this particular course mapped to a course in WeBWorK + $wwclient = new wwassignment_client(); + $wwcoursename = _wwassignment_mapped_course($COURSE->id,false); + $wwsetname = _wwassignment_mapped_set($this->_instance); + $wwusername = $USER->username; + + //create the instructor if necessary + $wwusername = _wwassignment_mapcreate_user($wwcoursename,$wwusername,'10'); + + //login the instructor + $wwkey = _wwassignment_login_user($wwcoursename,$wwusername); + + $wwinstructorlink = _wwassignment_link_to_instructor_auto_login($wwcoursename,$wwusername,$wwkey); + + $mform->addElement('link','instructor_page_link', + get_string('instructor_page_link_desc','wwassignment'), + $wwinstructorlink,get_string('instructor_page_link_name','wwassignment')); + + + if($wwsetname != -1) { + //we are doing an update, since an id exists in moodle db + $wwsetlink = _wwassignment_link_to_edit_set_auto_login($wwcoursename,$wwsetname,$wwusername,$wwkey); + $mform->addElement('link','edit_set',get_string('edit_set_link_desc','wwassignment'), + $wwsetlink,get_string('edit_set_link_name','wwassignment')); + $wwsetdata = $wwclient->get_assignment_data($wwcoursename,$wwsetname,false); + + $opendate = strftime("%c", $wwsetdata['open_date']); + $duedate = strftime("%c", $wwsetdata['due_date']); + $mform->addElement('static','opendate','WeBWorK Set Open Date',$opendate); + $mform->addElement('static','duedate','WeBWorK Set Due Date',$duedate); + } + + //define the mapping + $mform->addElement('header','set_initialization',get_string('set_initialization','wwassignment')); + + //name + $mform->addElement('text', 'name', get_string('wwassignmentname', 'wwassignment'), array('size'=>'64')); + $mform->setType('name', PARAM_TEXT); + $mform->addRule('name', null, 'required', null, 'client'); + + //set select + $options = $wwclient->options_set($wwcoursename,false); + $mform->addElement('select','webwork_set',get_string('webwork_set','wwassignment'),$options); + $mform->setHelpButton('webwork_set', array('webwork_set', get_string('webwork_set', 'wwassignment'), 'wwassignment')); + + //description + $mform->addElement('htmleditor', 'description', get_string('description', 'assignment')); + $mform->setType('description', PARAM_RAW); + $mform->setHelpButton('description', array('writing', 'questions', 'richtext'), false, 'editorhelpbutton'); + + $features = new stdClass; + $features->gradecat = true; + $features->groups = false; + $this->standard_coursemodule_elements($features); + + $this->add_action_buttons(); + //$this->standard_hidden_coursemodule_elements(); + return; + } +} + +?> \ No newline at end of file --- /dev/null +++ wwassignment4/moodle/mod/wwassignment/view.php @@ -0,0 +1,88 @@ +<?php +/** +* @desc Prints an actual wwassignment with an iframe to WeBWorK. +*/ + +require_once("../../config.php"); +require_once("locallib.php"); + +$id = optional_param('id', 0, PARAM_INT); // Course Module ID, or +$a = optional_param('a', 0, PARAM_INT); // NEWMODULE ID + +if($id) { + if (! $cm = get_record("course_modules", "id", $id)) { + error("Course Module ID was incorrect"); + } + + if (! $course = get_record("course", "id", $cm->course)) { + error("Course is misconfigured"); + } + if (! $wwassignment = get_record("wwassignment", "id", $cm->instance)) { + error("Course module is incorrect"); + } +} else { + + if (! $wwassignment = get_record("wwassignment", "id", $a)) { + error("Course module is incorrect"); + } + if (! $course = get_record("course", "id", $wwassignment->course)) { + error("Course is misconfigured"); + } + if (! $cm = get_coursemodule_from_instance("wwassignment", $wwassignment->id, $course->id)) { + error("Course Module ID was incorrect"); + } +} + +//catch the guests +global $USER; +if($USER->username == 'guest') { + print_error('Guests cannot view WeBWorK Problem Sets'); +} + +//force login +$courseid = $course->id; +$wwassignmentid = $wwassignment->id; +require_login($courseid); + +//webwork code +$wwcoursename = _wwassignment_mapped_course($courseid,false); +$wwusername = $USER->username; +$wwsetname = $wwassignment->webwork_set; +_wwassignment_mapcreate_user($wwcoursename,$wwusername); +_wwassignment_mapcreate_user_set($wwcoursename,$wwusername,$wwsetname); +$wwkey = _wwassignment_login_user($wwcoursename,$wwusername); +$wwsetlink = _wwassignment_link_to_set_auto_login($wwcoursename,$wwsetname,$wwusername,$wwkey); + +add_to_log($course->id, "wwassignment", "view", "view.php?id=$cm->id", "$wwassignment->id"); + +/// Print the page header + +if ($course->category) { + $navigation = "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->"; +} + +$strwwassignments = get_string("modulenameplural", "wwassignment"); +$strwwassignment = get_string("modulename", "wwassignment"); + +print_header("$course->shortname: $wwassignment->name", "$course->fullname", "$navigation <a href='index.php?id=$course->id'>$strwwassignments</a> -> $wwassignment->name", "", "", true, update_module_button($cm->id, $course->id, $strwwassignment), navmenu($course, $cm)); +/// Print the main part of the page + + + +// Print webwork in iframe and link to escape to have webwork in a single window +print("<p style='font-size: smaller; color: #aaa;'>" . get_string("iframeNoShow-1", "wwassignment") + . "<a href='$wwsetlink'>" . get_string("iframeNoShow-2", "wwassignment") + ."</a><p align='center'></iframe></p>\n" + ); +print("<iframe id='wwPage' src='$wwsetlink' frameborder='1' " + . "width='".$CFG->wwassignment_iframewidth."' " + . "height='".$CFG->wwassignment_iframeheight."'>" + ); + +print("<script>ww.Init(".isteacher($course->id).")</script>"); + + +/// Finish the page +print_footer($course); + +?> --- /dev/null +++ wwassignment4/moodle/mod/wwassignment/locallib.php @@ -0,0 +1,578 @@ +<?php +global $CFG; +require_once("$CFG->libdir/soap/nusoap.php"); +require_once("/Library/WebServer/Documents/moodle/lib/soap/nusoap.php"); +define('WWASSIGNMENT_DEBUG',0); + + +////////////////////////////////////////////////////////////////// +//Utility functions +////////////////////////////////////////////////////////////////// + +/** + * @desc prints message to the apache log + * @param string message +**/ + +function debugLog($message) { + if (WWASSIGNMENT_DEBUG) { + error_log($message); + var_dump($message); + } +} + +/** (reference from accesslib.php ) + * @desc gets all the users assigned this role in this context or higher + * @param int roleid (can also be an array of ints!) + * @param int contextid + * @param bool parent if true, get list of users assigned in higher context too + * @param string fields - fields from user (u.) , role assignment (ra) or role (r.) + * @param string sort - sort from user (u.) , role assignment (ra) or role (r.) + * @param bool gethidden - whether to fetch hidden enrolments too + * @return array() + */ +//function get_role_users($roleid, $context, $parent=false, $fields='', $sort='u.lastname ASC', $gethidden=true, $group='', $limitfrom='', $limitnum='') { + + +/** +* @desc Finds all of the users in the course +* @param $courseid -- the course id +* @return record containing user information ( username, userid) +*/ + +function _wwassignment_get_course_students($courseid) { + debugLog("Begin get_course_students($courseid )"); + $context = get_context_instance(CONTEXT_COURSE, $courseid); + //debugLog("context is ". print_r($context, true)); + + $users = array(); + $roles_used_in_context = get_roles_used_in_context($context); + foreach($roles_used_in_context as $role) { + $roleid = $role->id; + //debugLog( "roleid should be 5 for a student $roleid"); + $users = array_merge($users, get_role_users($roleid, $context, true) );//FIXME a user could be liseted twice + } + debugLog("display users in course--off"); + //debugLog(print_r($users, true)); + + debugLog("End get_course_students($courseid )"); + return $users; + + +} + +////////////////////////////////////////////////////////////////// +//EVENT CREATION AND DELETION +////////////////////////////////////////////////////////////////// + +/** +* @desc Creates the corresponding events for a wwassignment. +* @param $wwsetname string The name of the set. +* @param $wwassignmentid string The ID of the wwassignment record. +* @param $opendate integer The UNIX timestamp of the open date. +* @param $duedate integer The UNIX timestamp of the due date. +* @return integer 0 on success. -1 on error. +*/ +function _wwassignment_create_events($wwsetname,$wwassignmentid,$opendate,$duedate) { + global $COURSE; + unset($event); + $event->name = $wwsetname; + $event->description = 'WeBWorK Set Event'; + $event->courseid = $COURSE->id; + $event->groupid = 0; + $event->userid = 0; + $event->format = 1; + $event->modulename = 'wwassignment'; + $event->instance = $wwassignmentid; + $event->visible = 1; + + $openevent = $event; + $dueevent = $event; + + $openevent->name .= ' is Open.'; + $openevent->eventtype = 'open'; + $openevent->timestart = $opendate; + $openevent->timeduration = $duedate - $opendate; + + $dueevent->name .= ' is Due.'; + $dueevent->eventtype = 'due'; + $dueevent->timestart = $duedate; + $dueevent->timeduration = 1; + + + $result = 0; + if(!add_event($openevent)) { + $result = -1; + } + if(!add_event($dueevent)) { + $result = -1; + } + return $result; +} + + +/** +* @desc Deletes all events relating to the wwassignment passed in. +* @param $wwassignmentid integer The wwassignment ID. +* @return integer 0 on success +*/ +function _wwassignment_delete_events($wwassignmentid) { + if ($events = get_records_select('event', "modulename = 'wwassignment' and instance = '$wwassignmentid'")) { + foreach($events as $event) { + delete_event($event->id); + } + } + return 0; +} + +////////////////////////////////////////////////////////////////// +//Functions that ensure creation of WeBWorK Data +////////////////////////////////////////////////////////////////// + +/** +* @desc Checks whether a user exists in a WW course. If it doesnt creates the user using the currently logged in one. +* @param $wwcoursename string The WW course. +* @param $username string The username to check. +* @param $permission string The permission the user needs if created. +* @return string the new username. +*/ +function _wwassignment_mapcreate_user($wwcoursename,$username,$permission = '0') { + $wwclient = new wwassignment_client(); + $exists = $wwclient->mapped_user($wwcoursename,$username); + if($exists == -1) { + global $USER; + $tempuser = $USER; + $newusername = $wwclient->create_user($wwcoursename,$tempuser,$permission); + return $newusername; + } + return $username; +} + +/** +* @desc Checks whether a set exists for a user in a WW course. If it doesnt autocreates. +* @param $wwcoursename string The WW course. +* @param $wwusername string The WW user. +* @param $wwsetname string The WW set. +* @return integer 0. +*/ +function _wwassignment_mapcreate_user_set($wwcoursename,$wwusername,$wwsetname) { + $wwclient = new wwassignment_client(); + $exists = $wwclient->mapped_user_set($wwcoursename,$wwusername,$wwsetname); + if($exists == -1) { + $wwclient->create_user_set($wwcoursename,$wwusername,$wwsetname); + } + return 0; +} + +/** +* @desc Makes sure that a user is logged in to WW. +* @param $wwcoursename string The course to login to. +* @param $wwusername string The user to login. +* @return string The users key for WW. +*/ +function _wwassignment_login_user($wwcoursename,$wwusername) { + $wwclient = new wwassignment_client(); + return $wwclient->login_user($wwcoursename,$wwusername,false); +} + +//////////////////////////////////////////////////////////////// +//functions that check mapping existance in the local db +//////////////////////////////////////////////////////////////// + +/** +* @desc Finds the webwork course name from a moodle course id. +* @param integer $courseid Moodle Course ID. +* @param integer $silent whether to trigger an error message. +* @return string the name of the webwork course on success and -1 on failure. +*/ +function _wwassignment_mapped_course($courseid,$silent = true) { + $wwassignmentbridge = get_record('wwassignment_bridge','course', $courseid); + if((isset($wwassignmentbridge)) && (isset($wwassignmentbridge->webwork_course))) { + return $wwassignmentbridge->webwork_course; + } + if(!$silent) { + print_error('webwork_course_map_failure','wwassignment'); + } + return -1; +} + +/** +* @desc Finds the webwork set name from a wwassignment id. +* @param integer $wwassignmentid Moodle wwassignment ID. +* @param integer $silent whether to trigger an error message. +* @return string the name of the webwork set on success and -1 on failure. +*/ +function _wwassignment_mapped_set($wwassignmentid,$silent = true) { + $wwassignment = get_record('wwassignment','id',$wwassignmentid); + if((isset($wwassignment)) && (isset($wwassignment->webwork_set))) { + return $wwassignment->webwork_set; + } + if(!$silent) { + print_error('webwork_set_map_failure','wwassignment'); + } + return -1; +} + +//////////////////////////////////////////////////////////////// +//functions that create links to the webwork site. +//////////////////////////////////////////////////////////////// + +/** +* @desc Returns URL link to a webwork course logging the user in. +* @param string $webworkcourse The webwork course. +* @param string $webworkset The webwork set. +* @param string $webworkuser The webwork user. +* @param string $key The key used to login the user. +* @return URL. +*/ +function _wwassignment_link_to_edit_set_auto_login($webworkcourse,$webworkset,$username,$key) { + return _wwassignment_link_to_course($webworkcourse) . "instructor/sets/$webworkset/?effectiveUser=$username&user=$username&key=$key"; +} + + +/** +* @desc Returns URL link to a webwork course logging the user in. +* @param string $webworkcourse The webwork course. +* @param string $webworkuser The webwork user. +* @param string $key The key used to login the user. +* @return URL. +*/ +function _wwassignment_link_to_instructor_auto_login($webworkcourse,$username,$key) { + return _wwassignment_link_to_course($webworkcourse) . "instructor/?effectiveUser=$username&user=$username&key=$key"; +} + +/** +* @desc Returns the URL link to a webwork course and a particular set logged in. +* @param string $webworkcourse The webwork course. +* @param string $webworkset The webwork set. +* @param string $webworkuser The webwork user. +* @param string $key The key used to login the user. +* @return URL. +*/ +function _wwassignment_link_to_set_auto_login($webworkcourse,$webworkset,$webworkuser,$key) { + return _wwassignment_link_to_set($webworkcourse,$webworkset) . "?effectiveUser=$webworkuser&user=$webworkuser&key=$key"; +} + +/** +* @desc Returns the URL link to a webwork course and a particular set. +* @param string $webworkcourse The webwork course. +* @param string $webworkset The webwork set. +* @return URL. +*/ +function _wwassignment_link_to_set($webworkcourse,$webworkset) { + return _wwassignment_link_to_course($webworkcourse) . "$webworkset/"; +} + +/** +* @desc Returns the URL link to a webwork course. +* @param string $webworkcourse The webwork course. +* @return URL. +*/ +function _wwassignment_link_to_course($webworkcourse) { + global $CFG; + return $CFG->wwassignment_webworkurl."/$webworkcourse/"; +} + + +/////////////////////////////////////////////////////////////// +//wwassignment client class +/////////////////////////////////////////////////////////////// + +/** +* @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. +*/ +class wwassignment_client { + var $client; + var $defaultparams; + var $datacache; + var $mappingcache; + + /** + * @desc Constructs a singleton webwork_client. + */ + function wwassignment_client() + { + global $CFG; + // static associative array containing the real objects, key is classname + static $instances=array(); + // get classname + $class = get_class($this); + if (!array_key_exists($class, $instances)) { + // does not yet exist, save in array + $this->client = new soap_client($CFG->wwassignment_rpc_wsdl,'wsdl'); + $err = $this->client->getError(); + if ($err) { + print_error('construction_error','wwassignment'); + } + $this->defaultparams = array(); + $this->defaultparams['authenKey'] = $CFG->wwassignment_rpc_key; + $this->datacache = array(); + $this->mappingcache = array(); + $instances[$class] = $this; + + } + foreach (get_class_vars($class) as $var => $value) { + $this->$var =& $instances[$class]->$var; + } + } + + /** + *@desc Calls a SOAP function and passes (authenkey,course) automatically in the parameter list. + *@param string $functioncall The function to call + *@param array $params The parameters to the function. + *@param integer $override=false whether to override the default parameters that are passed to the soap function (authenKey). + *@return Result of the soap function. + */ + function handler($functioncall,$params=array(),$override=false) { + if(!is_array($params)) { + $params = array($params); + } + if(!$override) { + $params = array_merge($this->defaultparams,$params); + } + if(WWASSIGNMENT_DEBUG) { + echo "Called: $functioncall <br>"; + echo "Params: "; + var_dump($params); + echo "<br>"; + } + $result = $this->client->call($functioncall,$params); + + //$result = call_user_func_array(array(&$this->client,$functioncall),$params); + if($err = $this->client->getError()) { + //print_error(get_string("rpc_fault","wwassignment') . " " . $functioncall. " ". $err); + print_error('rpc_error','wwassignment'); + } + return $result; + } + ... [truncated message content] |