Installation

Batch-editing problem file locations

Re: Batch-editing problem file locations

by Andree Chea -
Number of replies: 0
Thanks for the pointers. I've written and tested the scripts on my laptop and it appears to be working (and I learned a few things in the process!), and will test on the webwork server within a few days. My scripts are here for whoever stumbles across this thread in the future and wants it (be sure to test first!):
#!/bin/sh

# Update Problem Library Locations
# by Andree Chea

# 20080526

# This script updates problem library locations, by changing prefixes whereever a problem file may be
# A detailed description as per this forum post: http://wwrk.maa.org/moodle/mod/forum/discuss.php?d=532
# This thread was found in the process: http://wwrk.maa.org/moodle/mod/forum/discuss.php?d=311

# changes symlinks in each course to the correct directory
# from http://webwork.maa.org/wiki/CVS_Access_to_Problem_Libraries

for each in /opt/webwork/courses/*/templates
do
 #change Library path
 cd $each

 course_name=`echo "$each" | awk -F/ '{ print $5 }'`

 echo $course_name

 if [ -h "Library" ] #if exists and is a symbolic link
 then
 rm -f Library
 ln -s /opt/webwork/libraries/ProblemLibrary Library
 fi

 if [ -h "Local" ] #local
 then
 rm -f Local
 ln -s /opt/webwork/libraries/local_problems Local
 fi

 # get list of def files containing Local at beginning
 # grep --files-with-matches --basic-regexp "^Local/*" `find . -type f`
 # http://www.linuxdevcenter.com/pub/a/linux/lpt/09_22.html - xargs with spaces in file names, not working...

 for file in $( find -P . -name "*.def" -type f | xargs grep --files-with-matches --basic-regexp "^Local/*" )
 do
 sed -e "s/^Local\//Library\//g" "$file" > tmp_file #replace all instances of ^Local\/ with Library/
 mv tmp_file "$file"
 done
done


exit
---
#!/usr/bin/php

<?php
 // Updates problem library locations in mysql
 // By Andree Chea

 // 20080602

 // To be used with update_pl_locations, which edits the courses directory
 // Should edit problem and problem_user tables


 // updates path results so that Local is library
 function updatePaths ( $m_table_list )
 {
 //can be problem_user or problem, doesn't matter
 //m_table_list is raw mysql output of SHOW TABLES
 //must do each table separately because of mysql error 1052

 while ( $table_name = mysql_fetch_array($m_table_list))
 {
 $query = "SELECT set_id, problem_id, source_file FROM " . $table_name[0] . " WHERE source_file LIKE '%Local%'";
 $results = mysql_query($query);
 //results has list of afflicted problems
 //change each row (Local to Library) and update
 echo $table_name[0] . " ... ";
 $count = 0;
 $fail = 0;
 while ($row = mysql_fetch_object($results))
 {
 $path_url = str_replace ( "Local/" , "Library/", $row->source_file ); //find and replace
 if (strcmp($path_url, $row->source_file) != 0)
 {
 //update
 $query = "UPDATE " . $table_name[0] . " SET source_file = \"" . $path_url .
 "\" WHERE ( set_id = \"" . $row->set_id . "\" AND problem_id = " . $row->problem_id . ")";
 //echo $query . "\n";
 $status = mysql_query($query);
 //var_dump($row);

 //update counters
 $count++;
 if (!($status))
 $fail++;

 }
 }

 echo $count . ($fail ? " failed: " . $fail : "" ) . "\n";
 }
 }

 function mysqlToString ( $results )
 {
 // http://www.webmasterworld.com/databases_sql_mysql/3276117.htm

 $text = array();
 $string = '';
 while(list($val) = mysql_fetch_array($results))
 {
 if ($val)
 {
 $text[] = $val;
 }
 };
 if (!empty($text))
 {
 $string = implode(", ",$text);
 return $string;
 }
 else
 return "";
 }

 $mysqlUser = "root";
 $mysqlPass = "testpass0";
 $mysqlDatabase = "webwork";

 // open connection to MySQL server
 $connection = mysql_connect(localhost,$mysqlUser,$mysqlPass)
 or die ('Unable to connect to MySQL!');
 // select database for use
 mysql_select_db($mysqlDatabase) or die ('Unable to select database');

 $query = "SHOW TABLES";
 /*$results = mysql_query($query); #list of all courses

 while ($course = mysql_fetch_array ($results))
 {
 echo $course[0] . "\n";
 }*/

 $result = mysql_query($query . " LIKE '%_problem'");
 //$table_list = mysqlToString($result);
 updatePaths ($result);
 $result = mysql_query($query . " LIKE '%_problem_user'");
 //$table_list .= ", " . mysqlToString($result);
 // table_list has all problem and problem_user tables, but can't do because of MYSQL error 1052
 //echo $table_list;
 updatePaths ($result);

 mysql_close($connection);
?>

edit: the forum software messed with the indents...