Installing Moodle 1.9 in Ubuntu 12.04

Step 1: Install and set up MySQL

  • sudo apt-get install mysql-server (assuming password ‘123456’)
  • mysql -u root -p123456
    • CREATE DATABASE moodle DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    • GRANT ALL PRIVILEGES ON moodle.* TO moodleuser@localhost IDENTIFIED BY ‘123456’;
    • GRANT SELECT,LOCK TABLES on moodle.* TO moodlebackup@localhost IDENTIFIED BY ‘123456’;
    • FLUSH PRIVILEGES;
    • QUIT

Step 2: Install Apache

  • sudo apt-get install apache2
  • sudo gedit /etc/apache2/httpd.conf
    • ServerName localhost
  • sudo chown -R www-data:www-data /var/www/
  • sudo /etc/init.d/apache2 restart

Step 3: Install PHP

  • sudo apt-get install php5-ldap php5-odbc
  • sudo apt-get install libapache2-mod-php5 php5-gd php5-mysql

Step 3: Install Moodle

  • cd /var/www
  • sudo wget http://download.moodle.org/stable19/moodle-latest-19.tgz
  • sudo tar -zxf moodle-latest-19.tgz
  • sudo mkdir /var/www/moodledata
  • sudo chown -R www-data:www-data /var/www/moodledata
  • sed -i ‘s:/var/www:/var/www/moodle:g’ /etc/apache2/sites-available/default
  • sudo /etc/init.d/apache2 restart
  • sudo chmod -R 755 /var/www/moodle/
  • sudo chown -R www-data:www-data /var/www/moodle

Step 4: Set up Moodle

References:

Advertisements

How to creates a free micro instance at Amazon

Step 1: Creates your AWS account

  1. Enter in http://aws.amazon.com/free/
  2. Creates an Amazon account, the same used to purchase books (requires a valid credit card)
  3. 3. Completes the form..
  4. Verifies your telephone

Step 2: Creates a micro instance

  1.  Enter in http://aws.amazon.com/free/
  2. Goes in ‘Account’ > ‘AWS Management Console’
  3. Click in EC2 (default hostage: US East N. Virginia)
  4. Click in ‘Launch instance’
  5. Select ‘Classic Wizard’
  6. Choose an image (Ubuntu 12.04 LTS)
  7. Selects instance ‘T1 Micro, 613MB Free Tier’ (On demand)
  8. Skips Advanced Instance Options (choose specific kernetl or RAM disks)
  9. Storage (8 GB)
  10. Skips Tags
  11. Createa key pair named ‘microaws’ (do NOT proceed without a Key Pair)
  12. Skips configure firewall (which will creates group quick-start-1, allowing SSH at 22)
  13. Review instance info and
  14. Launch it

Step 3: Access instance via ssh

  1. Change key pair file permission
    • chmod 600 microaws.pem
  2. Connects to remote host:

Step 4: Enables port 80

  1. Enter in http://aws.amazon.com/free/
  2. Goes in ‘Account’ > ‘AWS Management Console’
  3. Click on ‘Security Groups’
  4. Select group ‘quick-start-1’
  5. Click on tab ‘Inbound’
  6. Add rule > Create a new rule > HTTP
  7. Apply Rule Changes
  8. Tests port 80
    • telnet <EC2-HOSTNAME> 80

References:

Adding a Samsung ML-1865w laser printer to Ubuntu

Samsung ML-1865w laser printer is a good alternative for those who print much more in black and white than in color. I was trying to add it to Ubuntu, but it was not found automatically by Ubuntu. So I need to add its driver manually, following the steps below:

# enter in a temporarty directory
cd /var/tmp

# downloads the unified linux driver
wget http://org.downloadcenter.samsung.com/downloadfile/ContentsFile.aspx?CDSite=UNI_HK_EN&CttFileID=3182440&CDCttType=DR&ModelType=N&ModelName=ML-1865W&VPath=DR/201106/20110602161236937/UnifiedLinuxDriver_0.98.tar.gz

# unconpressing driver 
tar -xzf UnifiedLinuxDriver_0.98.tar.gz

# remove compressed drive
rm UnifiedLinuxDriver_0.98.tar.gz

# enter 
cd cdroot

# run autorun -> It will open a window -> Next -> I accept -> Next -> Add user: vianna -> Next -> Mark "Disable LPT support" -> Wait installing pr
sudo ./autorun

# remove install directory
cd ..; rm -rf cdroot

See ya!

Building Google Test Framework with CMake

For those who does not know GTest (Google Test Framework), it is a simple way to make unit tests in C and C++ source code.

But what is an unit test?

In GTest Documentation you will find an in-depth description, but in general terms, one unit test consists in a specific test of one function of one source file from an application. It has basically an initialization phase to allows you to call the correspondent function. Then it comprises on make some sanity tests (assertions) to check if your code is behaving as expected.

How GTest works?

GTest provides some assertion macros which simplify the tests as the following numerical comparison assertions (there is also some macros for binary and string comparison):

Fatal assertion Nonfatal assertion Verifies
ASSERT_EQ(expected, actual); EXPECT_EQ(expected, actual); expected == actual
ASSERT_NE(val1, val2); EXPECT_NE(val1, val2); val1 != val2
ASSERT_LT(val1, val2); EXPECT_LT(val1, val2); val1 < val2
ASSERT_LE(val1, val2); EXPECT_LE(val1, val2); val1 <= val2
ASSERT_GT(val1, val2); EXPECT_GT(val1, val2); val1 > val2
ASSERT_GE(val1, val2); EXPECT_GE(val1, val2); val1 >= val2

How to build a simple example?

The first step is download the source code and build the GTest library, which can be performed using g++ compiler (replace $(GTEST_DIR) by the place of GTest directory):

wget http://googletest.googlecode.com/files/gtest-1.6.0.zip
unzip gtest-1.6.0.zip
g++ -I ${GTEST_DIR}/include -I ${GTEST_DIR} -c ${GTEST_DIR}/src/gtest-all.cc
ar -rv libgtest.a gtest-all.o

It will generates the libraries libgtest.a (which contains GTest binaries). Consider a simple unit test example of a C source code named test1.c:

#include <gtest/gtest.h>
TEST(MathTest, TwoPlusTwoEqualsFour) {
EXPECT_EQ(2 + 2, 4);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest( &argc, argv );
return RUN_ALL_TESTS();
}

To build it, it is necessary to defines the GTest headers directory (parameter includes dir -I), compile the source code and link it with the GTest library (libgtest.a) and pthread, as shown below (again, replace $(GTEST_DIR) by the GTest directory):

g++ -I ${GTEST_DIR}/include test1.c libgtest.a -lpthread -o test1

The call and the output is as follows:

./test1
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MathTest
[ RUN      ] MathTest.TwoPlusTwoEqualsFour
[       OK ] MathTest.TwoPlusTwoEqualsFour (0 ms)
[----------] 1 test from MathTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.

How to build GTest samples?

Jointly with the GTest, it also comes some C++ unit test examples, found in the samples directory. You can build this examples using CMake a simpler and powerful tool to generate Makefiles. It can be installed using apt-get:

sudo apt-get install cmake

The CMake uses a configuration file named CMakeLists.txt. The common way to build a project with CMake is create a build directory, generate a Makefile using CMake and build it with make. One advantage of using CMake is that you separate the deploy from source code and can make the deploy in multiple places with the same CMake file.

cd gtest-1.6.0
mkdir build
cd build
cmake -Dgtest_build_samples=ON ..
make

Now you can execute all examples that comes GTest.

cd gtest-1.6.0/build
./sample1_unittest
./sample2_unittest
...
./sample10_unittest

How to build your own CMake?

A last tip is how to build a CMakeFile.txt for your project. Instead of use the CMake file provided by GTest, let’s make our own CMake to build the GTest’s sample 1 (Factorial test). The CMake file will requires to know where you unzip GTest, which can be performed through an environment variable named GTEST_ROOT (replace “/tmp/gtest-1.6.0” by the GTest directory).

GTEST_ROOT="/tmp/gtest-1.6.0"
export GTEST_ROOT

Then, creates a CMakeLists.txt with the content below:

cmake_minimum_required(VERSION 2.6)
project(sample1)
enable_testing()
find_package (Threads)
message(STATUS GTEST_ROOT=$ENV{GTEST_ROOT})
include_directories($ENV{GTEST_ROOT}/include)
link_directories($ENV{GTEST_ROOT}/mybuild)
add_executable(sample1 sample1.cc sample1_unittest.cc)
target_link_libraries(sample1 gtest gtest_main)
target_link_libraries(sample1 ${CMAKE_THREAD_LIBS_INIT})
add_test(NAME sample1 COMMAND sample1)

To build it you should repeat the same steps above:

mkdir build
cd build
cmake ..
make

The call and output of this example is as follows:

./sample1
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative (0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero (0 ms)
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (0 ms total)

[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative (0 ms)
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial (0 ms)
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (0 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (1 ms total)
[  PASSED  ] 6 tests.

See ya!

Shuffling the lines of a large file

There is a lot of methods for sampling a dataset. One simple way is the random sampling, where you shuffle the instances of a collection, here represented by the lines of a file. According to the size of your file it can be performed using the ‘shuf’ tool, provided by linux coreutils. One example of common usage is given above:

shuf [input-file] > [output-file]

However, this tool requires that all the input size fits in the memory. If your file size exceeds the memory size, you can use the linux ‘sort’ with parameter ‘-R’. An example of its usage is as follows:

sort -R [input-file] > [output-file]

Recall that in some environments the sort does not work properly for case sensitive texts (as is highlighted in sort manual). If this is your case, you may execute the following command before the sorting:

export "LC_ALL=C"

See ya!

How to transpose a single table row to multiple rows in MySQL

Some weeks ago I posted how to transpose in MySQL multiple table rows of a
given column to a single table row (separated by a DELIMITER) following a given
group criteria using ‘group_concat’. I also need some times the opposite, to break
one field with several data separated by a DELIMITER (ex: “blog,social-network,rss”)
in several rows. Consider the same example of the previous post, where we have
a list of tags in the format as follows:

-- +-----------------------------+--------------------------+
-- | url                         | tags                     |
-- +-----------------------------+--------------------------+
-- | http://grooveshark.com      | music,social-network     |
-- | https://lembra.wordpress.com | blog,social-network,rss  |
-- | http://stackoverflow.com    | questions,social-network |
-- +-----------------------------+--------------------------+
-- 3 rows in SET (0.00 sec)

And we want to pass it to the following format:

-- +-----------------------------+----------------+
-- | url                         | tag            |
-- +-----------------------------+----------------+
-- | http://grooveshark.com      | music          |
-- | http://grooveshark.com      | social-network |
-- | https://lembra.wordpress.com | blog           |
-- | https://lembra.wordpress.com | social-network |
-- | https://lembra.wordpress.com | rss            |
-- | http://stackoverflow.com    | questions      |
-- | http://stackoverflow.com    | social-network |
-- +-----------------------------+----------------+
-- 7 rows in SET (0.00 sec)

I do not known any feature of MySQL that provides that behavior directly, inspired
by Marco Gonçalves’s blog, I implement a generic procedure called ‘split_column’,
which receive the name of the table, the field that you want to break (ex: ‘tags’)
and the DELIMITER (ex: ‘,’). Meanwhile in the Marco’s procedure it uses a static
table assignment, which requires to be changed (changing table name and column name)
each time you need to use.

By the way there is a constraint in MySQL that avoid to use dynamic SQL (a query
built by an string dinamicaly) for cursor declaration (this is performed in compilation
time). Meanwhile it is possible to create a dynamic SQL to a table view. So I
create a procedure that creates a view (a logical table representation, it is not
materialized in physical disk) to the target field of the input table and use
this view statically in the Marco’s procedure. The output is stored in a table
with name given by the break column name followd by a ‘list’ suffix (ex: ‘tags_list’).


-- Creates this only once -- 

DELIMITER $$
DROP PROCEDURE IF EXISTS create_view $$
CREATE PROCEDURE create_view( table_name VARCHAR(256), field_name VARCHAR(256), prefix VARCHAR(256) ) 
BEGIN   
  DROP VIEW IF EXISTS table_view;    
  SET @stm = CONCAT( 'CREATE VIEW table_view AS SELECT ', field_name, ', ', prefix, ' FROM ', table_name );   
  PREPARE stmt FROM @stm;   
  EXECUTE stmt;   
  DEALLOCATE prepare stmt; 
END; $$
DELIMITER ;

DELIMITER $$
DROP PROCEDURE IF EXISTS split_table $$
CREATE PROCEDURE split_table( field_name VARCHAR(256), prefix VARCHAR(256), delim VARCHAR(16) ) 
BEGIN   
  DECLARE read_field VARCHAR(256);   
  DECLARE read_prefix VARCHAR(256);
  DECLARE occurance int default 0;   
  DECLARE i INT DEFAULT 0;   
  DECLARE splitted_field VARCHAR(60);   
  DECLARE done int default 0;   
  DECLARE cur CURSOR FOR SELECT * FROM table_view;    
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;    
  SET @stm = CONCAT( 'DROP TABLE IF EXISTS ', field_name, '_list' );   
  PREPARE stmt FROM @stm;   
  EXECUTE stmt;   
  DEALLOCATE PREPARE stmt;    
  SET @stm = CONCAT( 'CREATE TABLE ', field_name, '_list( ', prefix, ' VARCHAR(256), ', field_name, ' VARCHAR(256) )' );   
  PREPARE stmt FROM @stm;   
  EXECUTE stmt;   
  DEALLOCATE PREPARE stmt;     
  OPEN cur;     
    read_loop: LOOP
      FETCH cur INTO read_prefix, read_field;                    
      IF done THEN         
        LEAVE read_loop;       
      END IF;         
      SET occurance = (SELECT LENGTH(read_field) - LENGTH(REPLACE(read_field, delim, '') ) + 1 );                 
      SET i = 1;
      WHILE i <= occurance DO         
        SET splitted_field = REPLACE( (SELECT REPLACE(SUBSTRING(SUBSTRING_INDEX(read_field, delim, i), length(SUBSTRING_index(read_field, delim, i - 1)) + 1), ',', '') ), delim, '');          
        SET @stm = CONCAT( 'INSERT INTO ', field_name, '_list VALUES ("', read_prefix, '", "', splitted_field, '")' );         
        PREPARE stmt FROM @stm;         
        EXECUTE stmt;         
        DEALLOCATE PREPARE stmt;                  
        SET i = i + 1;       
      END WHILE;        
    END LOOP;   
  CLOSE cur;    
END; $$
DELIMITER ;

DELIMITER $$
DROP PROCEDURE IF EXISTS split_column $$
CREATE PROCEDURE split_column( table_name VARCHAR(256), field_name VARCHAR(256), prefix VARCHAR(256), delim VARCHAR(16) ) 
BEGIN   
  CALL create_view( table_name, prefix, field_name );   
  CALL split_table( field_name, prefix, delim );   
  DROP VIEW IF EXISTS table_view;
END; $$
DELIMITER ;

-- Testing the split procedure -- 

DROP TABLE IF EXISTS page_tags;
CREATE TABLE page_tags(url varchar(32), tags varchar(64) );
INSERT INTO page_tags VALUES 
  ('http://grooveshark.com', 'music,social-network'),
  ('https://lembra.wordpress.com', 'blog,social-network,rss'),
  ('http://stackoverflow.com', 'questions,social-network');

SELECT * FROM page_tags;

CALL split_column( 'page_tags', 'tags', 'url', ',' );

SELECT * FROM tags_list;

See ya!

Sync Liferea and Google Reader

Recently Liferea`Blog realeased a new version that allows RSS synchronization with Google Reader.

Why this is useful?

This is nice if you work in more than one computer, to share your RSS and sync your read feeds.

Why use both? Liferea and Google Reader?

Some time ago I posted about Liferea, when I was moving from web to client applications (Gmail/Thunderbird, GoogleReader/Liferea, Twitter-Facebook-GoogleBuzz/Gwibber). One thing that motivated me to use client applications was the replacement of Ubuntu` notification area by mini-indicator, which comprises all this tools. Then, instead of having to access these sites several times (e-mail, RSS, social network) I’ve been using notification area.

Backing up feed list

Open Liferea and goes in ‘Subscriptions/Export feed list’.

Installing/Updating Liferea

Since current available Liferea version in apt-get repositories is 1.6.4, which does not have Google Reader synchronization feature, you can build current Liferea stable version (1.6.6) from source, as follows (I needed to install additional libraries for Ubuntu 11.04):

user@host:~$ wget http://sourceforge.net/projects/liferea/files/Liferea%20Stable/1.6.6/liferea-1.6.6b.tar.gz/download
user@host:~$ tar -xzf liferea-1.6.6b.tar.gz
user@host:~$ cd liferea-1.6.6b
user@host:~$ sudo apt-get install libxslt1-dev libwebkit-dev libglade2-dev

# For Ubuntu 10.04 I had to install the following dependencies (if this is not your case skip next line):
user@host:~$ sudo apt-get install intltool libsqlite3-dev libgconf2-dev

user@host:~$ ./configure
user@host:~$ make
user@host:~$ sudo make install

See ya!